【问题标题】:Laravel search query with Input array带有输入数组的 Laravel 搜索查询
【发布时间】:2013-05-27 16:15:16
【问题描述】:

我是laravel的初学者,我做了一个搜索查询,我也有标签要搜索,但我不知道如何搜索它

查询看起来像这样

public function post_search()
    {
        $tags = Input::get('tag_id');

        $search =  $this->user
                        ->with('tag')
                        ->where('username', 'LIKE', '%'.Input::get('username').'%')
                        ->where('first_name', 'LIKE', '%'.Input::get('first_name').'%')
                        ->where('last_name', 'LIKE', '%'.Input::get('last_name').'%')
                        ->where('group_id', 'LIKE', '%'.Input::get('group_id').'%')
                        ->where('tag_id', 'LIKE', '%'.$tags.'%')
                        ->paginate(22);

        $this->layout->title   = "Search results";
        $this->layout->content = View::make("home.results")
                               ->with('users', $search);

    }

并且 tag_id 输入返回以下内容

array(5) {
  [0]=>
  string(2) "20"
  [1]=>
  string(1) "1"
  [2]=>
  string(1) "3"
  [3]=>
  string(1) "2"
  [4]=>
  string(2) "15"
}

请有人给我一个提示,我可以如何在我的搜索中实现标签?

谢谢

编辑

我收到这个标签错误

Array to string conversion

【问题讨论】:

  • 我很困惑,你的问题到底是什么?

标签: php search laravel


【解决方案1】:

在您的查询中,您有

->where('tag_id', 'LIKE', '%'.$tags.'%')

$tags 是一个数组,但你将它与like 一起使用,所以你得到Array to string conversion 错误。你应该使用

->whereIn( 'tag_id', $tags );

Laravel Documentation.

【讨论】:

    猜你喜欢
    • 2016-07-25
    • 2017-07-14
    • 2017-01-12
    • 1970-01-01
    • 1970-01-01
    • 2021-10-26
    • 2017-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多