Skip to content

Instantly share code, notes, and snippets.

@Regis011
Created November 15, 2018 15:47
Show Gist options
  • Select an option

  • Save Regis011/80b464113be66ae4c643dc9a0cc2ebbc to your computer and use it in GitHub Desktop.

Select an option

Save Regis011/80b464113be66ae4c643dc9a0cc2ebbc to your computer and use it in GitHub Desktop.
Laravel - search controller function for multiple keywords and long text in form query. Eloquent search query filters.
public function search(Request $request)
{
$res = (new BestCategory)->newQuery();
$q = $request->q;
$q = explode(' ', $q);
$res->where('title', 'like', '%'.$q[0].'%')->orWhere('description', 'like', '%'.$q[0].'%');
for ($i=1; $i < count($q); $i++) {
$res->orWhere('title', 'like', '%'.$q[$i].'%');
$res->orWhere('description', 'like', '%'.$q[$i].'%');
}
return $res->get();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment