Udev's avatar
Level 2

How to use ``->filter()`` on eloquent collection

I normally use where function to filter queries as in the example.

->when($request->name, function($query, $name){
	$query->where('name',  ' like',  '%'.$name.'%');
})

going through some examples as in the inertia pingCRM demo, I see they are using:

->filter(Request::only('search', 'trashed'))

Could someone explain how this works. The examples I've found on using ->filter() are something like:

->filter(function($user){
	if($request->has('name'))
		if(preg_match('/'.$request->name.'/', $user->name))
			return $user;
})
0 likes
1 reply

Please or to participate in this conversation.