Dmitry Malys Dmitry Malys - 2 months ago 11
PHP Question

Array to string conversion in Laravel using 'LIKE' query

sorry for a newbie question.
I run into a problem. I want to search the database for values which an in array.
But I have an error:


Array to string conversion


And kinda get why but, I don't know how to make it right. Can you help me please?
This is my code:

public function chassis($chassis){
return $this->builder->whereIn('model_type_en', 'LIKE', (array)"%$chassis%");
}


P.S please don't laugh at me :)

Answer Source
$collection = DB::table('your_table')->select('*');
foreach($chassis as $key=>$val) {
    if($key == 0) {
        $collection->where('model_type_en', 'like', "%$val%"));
    }
    $collection->orWhere('model_type_en', 'like', "%$val%"));
}
$name = $collection->get();

This may work. You can also look at the ref: laravel querybuilder how to use like in wherein function

(original wrong ans:)

If chassis is a string, you can do this:

$this->builder->where('model_type_en', 'LIKE', "%$chassis%");

You can read the docs: https://laravel.com/docs/5.4/queries