in Laravel

Algolia Custom Query Import With Laravel

In this tutorial we're going to create a Laravel command we can use to import queried models into Algolia search index by using Laravel scout.

What Is Laravel Scout?

Laravel scout is a simple package that allows you to quickly use a service like Algolia which will quickly allow you to add full test searching to your eloquent models. To install Laravel scout all you have to do is use composer to include it.


composer require laravel/scout

Then when the package is installed you need to add the trait to the models that you want to be searchable.


namespace App;

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

class Post extends Model
{
    use Searchable;
}

How To Import All Models

Laravel scout comes with handy command that helps you import a whole model into Algolia, if the model has the Searchable trait. All you have to do is use this command.


php artisan scout:import "App\Post"

This will then import all of the posts you have in your project. The problem with this is that it doesn't take into account any global scopes that you might have on the models. For example for blogs post you might have different status for published, drafts and scheduled posts you might not want drafts and scheduled posts to be imported in Algolia.

How To Import Queried Data?

In the next section we'll look into how we can create our own command to import only the published posts into Algolia. For the blog posts it's likely we'll have a global scope to make sure we only query for published posts to make sure we guard against draft posts being displayed on the front end. Because of this global scope we need to create our own command to query for only published posts and import them into Algolia. To create a new command on Laravel you can use the artisan make command


php artisan make:command ImportPostsAlgolia

This will create a new command in the app console command folder. Then we can add the below code into the command.


namespace App\Console\Commands;

use Illuminate\Console\Command;

class ImportPostsAlgolia extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'blog:import-posts-algolia';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Import only the published posts into algolia';

    /**
     * @var PostRepository
     */
    private $repository;

    /**
     * Add all published posts to algolia
     */
    public function handle()
    {
        $this->info('Importing posts');
        Post::published()->get()->searchable();
        $this->info('Posts imported');
    }
}

The important part of this command is this line Post::published()->get()->searchable(); so let's see what it's doing. First it's called Post::published() this is calling a new scope what we can add to Post model. You can use this scope by adding the following code to your Post model.


/**
 * @param Builder $query
 *
 * @return $this
 */
public function scopePublished(Builder $query)
{
    return $query->where('status', 'published');
}

Then we call the get() method which will query the database for posts which are published. Because we added the Searchable trait to the Post model it means we have access to a searchable() method, this method will take the post from the query and import them into Algolia. And that's it we've now created a command that makes a database query and them imports the results into Algolia search index so we can only search for published posts.

Subscribe To The Weekly Newsletter

Get weekly updates to your email