Search code examples
laravellaravel-scout

Advanced Laravel Search Functionality Using Scout


I have a Model named Profile in my project and i want to search user's full name through the records using Laravel Scout.

Search Function

public function SearchUser(Request $request)
{
    $searchProfileQuery = Profile::search($request->input('query'))->get();
    
    return view('users/search-result', compact('searchProfileQuery'));
}

Let's say there are 2 records :

  • Ben Hubble
  • Dean Russel

Current Condition :

  • If i search using 'e' keyword, both records are shown
  • If i search using 'dean' or 'Dean', only Dean Russel will be shown

QUESTION :

  • How if i misspelled the keyword 'Dean' with 'Dena' but Dean Russel still shown?
  • Or how if i make the keyword with excessive vowels like 'Been' but Ben Hubble still shown?

Thanks in advance.


Solution

  • If you use the database engine as laravel scout driver, the comparison is done by executing a SQL LIKE query against the desired fields as described here: https://laravel.com/docs/9.x/scout#database-engine

    What you'd like to have is an additional feature that is not part of the database engine. If switching to another engine like algolia is possible, please have a look at the Algolia typo tolerance documentation: https://www.algolia.com/doc/api-reference/api-parameters/typoTolerance/