Fuzzy Search in Laravel: A Complete Guide to Implementation.

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\[email protected]')
          ->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

Include Related Models in Search Results:

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!. If this was helpful you will definitely find these Laravel based articles helpful too!. Check them out.

Also, post any questions or suggestions that you may have in the comment section below. I’m always looking forward to helping and interact with other developers!.

0 0 vote
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments