Sapnesh Naik
Senior Full Stack Software Developer
Full stack software developer with 6 years of experience building highly scalable web applications using using backend, frontend, and cloud-based technologies.

Fuzzy Search in Laravel: A Complete Guide to Implementation.

March 9, 2020
Fuzzy Search in Laravel: A Complete Guide to Implementation.

A fuzzy search is a technique of finding relevant search results for an input argument even when the input argument does not precisely match the desired search results. I recently had to implement a fuzzy search in Laravel for my User model. This API would enable the user to search multiple fields in my User model without having to specify the exact value of a field!.

Implementing a fuzzy search in Laravel is easier than you would think!. With the vast community behind Laravel and its numerous third party packages, I was able to get a fuzzy search API for my User model up in no time!. Let me show you how.

Project Set-up:

  • For this tutorial, I have set up a Laravel 6 project. However, this tutorial holds good for any Laravel version greater than 5.
  • I have also generated 500 random records for the user model by using Laravel factories and database seeding.
  • The Laravel project is an API based project as the next part of this tutorial is going to be consuming this API using a Vue.js SPA!. However, you can still easily adapt the steps shown to enable a fuzzy search in a traditional Laravel Project!.

Install the Composer Package:

Let’s install the awesome Laravel Searchy package into our application using Composer.

composer require tom-lingham/searchy

Add the SearchyServiceProvider entry to the $providers array in Laravel’s ./config/app.php file:

TomLingham\Searchy\SearchyServiceProvider::class

Add the Searchy alias to the aliases array in Laravel’s ./config/app.php file so this we can readily access it in our application.

'Searchy' => TomLingham\Searchy\Facades\Searchy::class

Define the API Endpoint:

In your api.php add your search endpoint:

Route::get('/user-search', '\App\Http\Controllers\UserController@search')
          ->name('users.search');

Define the Fuzzy Search logic in Controller Method:

You need to pass the input to the search method as a query parameter in the URL itself. The search function will fetch this query param and run the search on our User model.

  public function search(Request $request)
  {
      $value = $request->input('value');

      return Searchy::users(
          'login_id',
          'email'
      )->select(
          'id',
          'email'
      )->query($value)
          ->getQuery()
          ->limit(50)
          ->get();
  }

Test the API:

Let’s fire up a curl request to the user-search API endpoint.

curl -X GET '127.0.0.1/api/v1/user-search?value=adrian'

Output:

Fuzzy Search in Laravel

The Laravel Searchy package returns a PHP Collection and provides no straight forward way of fetching related models fluently. But I figured out a way of using Eloquent to do this.

Model::hydrate() can be used to generate an Eloquent Collection from an array of objects.

public function searchU(Request $request)
{
    $value = $request->input('value');

    return User::hydrate(return Searchy::users(
        'login_id',
        'email'
    )->select(
        'id',
        'email'
    )->query($value)
        ->getQuery()
        ->limit(50)
    ->get()->toArray());
}

That’s it! it was that easy to add a fuzzy search to your Laravel project!.