Laravel Scout

Laravel Scout

Written by Dev Dojo on Oct 12th, 2016 Views Report Post

If you are new to Laravel 5.3 you may have heard about this thing called Laravel Scout. Laravel Scout provides a seamless driver based solution for adding a full-text search option to all Eloquent models.

With the help of model observers, Scout also automatically syncs search indexes with Eloquent records.

Though it is not included in Laravel by default because it is not an official package, it can still be pulled in via Composer and used with your Laravel App. As of right now, Laravel Scout is shipped with an Algolia driver, but developers are free to write their own custom drivers and extend Scout with personalized search implementations.

Installing Laravel Scout

To install, first of all, you need to pull in the package (Make sure it is live on a Laravel 5.3 app)

composer require laravel/scout

The next installation step is to add Scout service provider to the providers section. To do that, run the following command, and then copy the Algolia credentials to the Scout configuration.

php artisan vendor:publish

Now, all you need to do is install Algolia SDK.

composer require algolia / algoliasearch – client – php

Making the model ready for indexing

Next, go to the model, import the Laravel Scout’s searchable trait and optionally define the searchable properties and the name of the model’s index. Here is an example of what a Video model may look like with the searchable trait:

<?php

namespace App;

use Laravel\Scout\Searchable;
use Illuminate\Database\Eloquent\Model;

class Video extends Model
{
    use Searchable;
}

After doing this, check the Algolia index on the website, and you will notice that everytime a record is added, updated, or deleted from the records, the Algolia index updates as well.

Searching indexes

The output of Laravel Scout searches a collection of Eloquent models from the database. With the ids that are stored in Algolia, a list of matched id is returned. Laravel Scout takes out the specific database records matching those ids, and outputs them as Eloquent objects.

Queuing

For every request that is made to modify the database, an HTTP request to Algolia is made, and this obviously slows down the whole process. That is why queuing operations is a great alternative.

Developers can just set queue to true for config/scout.php, and all the updates will be indexed asynchronously.

In conclusion

Laravel developers have found great success with Laravel Scout and that is all because of its easy third party library integrations, a very functional core, and an always growing developer community. And we are positive that it will only grow more popular. Be sure to checkout the full Laravel Scout documentation to learn more.

Comments (0)