DEV Community

Fatimah for Summitech Computing Ltd

Posted on

Laravel: How to configure multiple search drivers

In this tutorial, we will look at how to configure multiple search drivers in Laravel. As anyone familiar with Laravel knows, Algolia is the officially supported search driver, it is the only search engine that ships out of the box with Laravel scout.

However, because Algolia can get pretty expensive, we mostly turn to other free alternatives till it is absolutely required to use Algolia.


This is not a from-the-scratch tutorial so here are a couple of assumptions I am making:


You want to reduce your cost to the barest minimum by using Algolia for specific models with expensive search operations while the remaining models use the free alternative you have setup.

  • Install algolia sdk via composer

composer require algolia/algoliasearch-client-php:^2.2

  • Add your app_id and secret to the .env file. To get this, login to your algolia account, go to API Keys and copy your application id and admin api key so you have something like this in your .env file
ALGOLIA_APP_ID=<application id>
ALGOLIA_SECRET=<admin api key>
  • Configure algolia in your scout.php file:
'algolia' => [
        'id'     => env('ALGOLIA_APP_ID', ''),
        'secret' => env('ALGOLIA_SECRET', ''),

and also make sure the other search driver you have setup is your default e.g.

'driver' => mysql

  • Configure algolia for a particular model:

Laravel scout has a searchableUsing() method which is responsible for getting the appropriate scout engine for the model and this is what we'll be overriding.

Right now, you can check the scout engine a model uses using artisan tinker like so:


php artisan tinker



in my case, it returns


which is the engine I am currently using.

Now we have that confirmed, we can go ahead to override the method

public function searchableUsing()
   return app(EngineManager::class)->engine('algolia');

The algolia in this case is from the engine we setup in our scout.php file i.e. the name we put in our engine must match what's stated in the scout config file. To avoid mistakes, we can add this to a constants file or a .env then pick from there.

You can verify that the override was successful by checking as we did earlier with the tinker environment.

So you can go ahead to (re)import your records and everything should work as normal.

Top comments (0)