Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel search query with Input array

I am a begginer with laravel and i made a search query, and i have tags to search for too, but i dont know how to search for it

Query looks like this

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);

    }

And the tag_id input returns the following

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

Could please someone give me a hint how i can implement the tags in my search?

Thank you

EDIT

I am getting this error with tags

Array to string conversion
like image 553
Web Student Avatar asked Oct 05 '22 00:10

Web Student


1 Answers

In your query you have

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

and $tags is an array but you used it with like so you got Array to string conversion error. You should use

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

Laravel Documentation.

like image 147
The Alpha Avatar answered Oct 09 '22 19:10

The Alpha