Laravel Scout

Laravel Scout has a first-party Meilisearch driver, so a CelerSearch instance works with it out of the box. Scout keeps your search index in sync with your Eloquent models; CelerSearch runs the engine.

1. Install

Shell
composer require laravel/scout
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
composer require meilisearch/meilisearch-php http-interop/http-factory-guzzle

2. Configure

Copy the Endpoint URL and API key from your instance's Connection details at app.celersearch.com, and set them in .env:

.env
Shell
SCOUT_DRIVER=meilisearch
MEILISEARCH_HOST=https://your-endpoint-url
MEILISEARCH_KEY=your-api-key
SCOUT_QUEUE=true

SCOUT_QUEUE=true sends index updates through your queue, so saving a model never waits on a network call. It's optional, but recommended in production.

Server-side only

The API key has full access to your instance. Scout uses it from your server, which is exactly where it belongs. Never pass it to your frontend.

3. Make a model searchable

app/Models/Post.php
PHP
<?php

namespace App\Models;

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

class Post extends Model
{
    use Searchable;

    /**
     * Get the indexable data array for the model.
     *
     * @return array<string, mixed>
     */
    public function toSearchableArray(): array
    {
        return [
            'id' => $this->id,
            'title' => $this->title,
            'body' => $this->body,
            'category' => $this->category,
            'created_at' => $this->created_at->timestamp,
        ];
    }
}

4. Configure index settings

Attributes you filter on with where must be filterable, and attributes you sort on with orderBy must be sortable. Declare them in config/scout.php:

config/scout.php
PHP
use App\Models\Post;

'meilisearch' => [
    'host' => env('MEILISEARCH_HOST', 'http://localhost:7700'),
    'key' => env('MEILISEARCH_KEY', null),
    'index-settings' => [
        Post::class => [
            'filterableAttributes' => ['category'],
            'sortableAttributes' => ['created_at'],
        ],
    ],
],

Push the settings to your instance, then import your existing records:

Shell
php artisan scout:sync-index-settings
php artisan scout:import "App\Models\Post"

Add scout:sync-index-settings to your deploy process so settings changes reach the instance.

PHP
use App\Models\Post;

// Typo-tolerant full-text search
$posts = Post::search('larvel scuot')->get();

// Filter, sort and paginate
$posts = Post::search($request->input('query'))
    ->where('category', 'tutorials')
    ->orderBy('created_at', 'desc')
    ->paginate(15);

From here on, Scout indexes models as they're created, updated and deleted.

Plans and limits

Every record Scout imports counts as one document toward your plan's document limit. Searches are always unlimited. If you reach the limit, new writes are refused until you delete documents or move to a larger plan. See Connect Any App.

Next steps