form models
{
//filters search
if(isset($filters['search']) ? $filters['search'] : false){
return $query->where('tittle','like','%' . $filters('search') . '%')
->orWhere('body', 'like', '%' .$filters('search') . '%');
}
}
and from controller
"posts" => Post::latest()->filter(request(['search']))->get()
and i get Array callback must have exactly two elements
i just learning laravel 8 please help me
3
Answers
just change it $filters['search'] not $filters('search')
You need to fix your conditional check inside the if block, you are doing extra work there. Rather than using
isset
you can useempty
This will check if the key exists and it is not null and empty too. So it will be like:Also, if you are trying to fetch search request from the request then you should just use
request('search)
. And in the filter method (scope). Are you sure that you need array there since you are passing a single search value, isrequest('search)
an array?