Laravel raw FULLTEXT request similar to Facebook Search

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?

+3
source share
1

. :

$input = Input::get('input');

$exp = explode(' ', $input);

$s = '';
$c = 1;
foreach ($exp AS $e)
{
    $s .= "+$e*";

    if ($c + 1 == count($exp))
        $s .= ' ';

    $c++;
}

$query = "MATCH (first_name, last_name, email) AGAINST ('$s' IN BOOLEAN MODE)";
// $query looks like 
// MATCH (first_name, last_name, email) AGAINST ('+jar* +eitni*' IN BOOLEAN MODE)

$users = User::whereRaw($query)->get();
+5

All Articles