The search filter

Sometimes the default search behavior may not meet your requirements. If this is the case, skip using the searchable() method on columns and define your own behavior directly on the query.

1public function query(): Builder
2{
3 return User::query()
4 ->when($this->getFilter('search'), fn ($query, $term) => $query->where('name', 'like', '%'.$term.'%')
5 ->orWhere('email', 'like', '%'.$term.'%'));
6}

You can make this even more streamlined by adding a search scope to your model like so:

1public function scopeSearch($query, $term)
2{
3 return $query->where(
4 fn ($query) => $query->where('name', 'like', '%'.$term.'%')
5 ->orWhere('email', 'like', '%'.$term.'%')
6 );
7}

And then using it like this:

1public function query(): Builder
2{
3 return User::query()
4 ->when($this->getFilter('search'), fn ($query, $term) => $query->search($term));
5}