I am creating an AJAX search function similar to Facebook in Laravel 4.1. The search field should search the table usersagainst first_name, last_nameand email.
I can enter any of the following terms or parts of terms such as:
- Jared
- Eitnier
- Jared Aitnier
- jeitnier@domain.com
- Ja
- jeit
- eitn
First I tried:
User::where('email', 'LIKE', $search)
->orWhere('first_name', 'LIKE', $search)
->orWhere('last_name', 'LIKE', $search)
->get();
But it just looks like one of the words. Then I tried to blow up the input space and create a query like this:
User::whereRaw("MATCH (first_name, last_name, email)
AGAINST ('$explode[0]' IN BOOLEAN MODE)
AND MATCH (first_name, last_name, email)
AGAINST ('$explode[1]' IN BOOLEAN MODE)")
->get();
which works but only gets exact matches ('jared', 'eitnier' or 'jared eitnier')
What do I need to change to get a match LIKE, but with any combination of the entered terms?
source
share