I'm implementing a search bar that can search customers by first name, last name, or both. So, for example, Mike Hizer
would be matched by Mike
, Hizer
, zer
, Mike Hizer
, etc. Here's what I came up with:
Customer::where(
DB::raw('concat(first_name," ",last_name)'), 'like', "%{$request->search}%"
)->get()
It works. But is there a more Eloquent approach to combine these two columns (first_name
and last_name
) without resorting to the DB facade? Would something like
->where(['first_name','last_name'], 'like', "%{$request->search}%")
be possible to achieve?
Try this way..
If you don't want to use the
DB
facade you could use the whereRaw method:Hope this helps!