Laravel Scout guide
Learn how to use Meilisearch with Laravel Scout.
In this guide, you will see how to setup Laravel Scout to use Meilisearch in your Laravel 10 application.
Prerequisites
Before you start, make sure you have the following installed on your machine:
- PHP
- Composer
You will also need a Laravel application. If you don’t have one, you can create a new one by running the following command:
Installing Laravel Scout
Laravel comes with out-of-the-box full-text search capabilities via Laravel Scout.
To enable it, navigate to your Laravel application directory and install Scout via the Composer package manager:
After installing Scout, you need to publish the Scout configuration file. You can do this by running the following artisan
command:
This command should create a new configuration file in your application directory: config/scout.php
.
Configuring the Laravel Scout driver
Now you need to configure Laravel Scout to use the Meilisearch driver. First, install the dependencies required to use Scout with Meilisearch via Composer:
Then, update the environment variables in your .env
file:
Local development
Laravel’s official Docker development environment, Laravel Sail, comes with a Meilisearch service out-of-the-box. Please note that when running Meilisearch via Sail, Meilisearch’s host is http://meilisearch:7700
(instead of say, http://localhost:7700
).
Check out Docker Bridge network driver documentation for further detail.
Running in production
For production use cases, we recommend using a managed Meilisearch via Meilisearch Cloud. On Meilisearch Cloud, you can find your host URL in your project settings.
Read the Meilisearch Cloud quick start.
If you prefer to self-host, read our guide for running Meilisearch in production.
Making Eloquent models searchable
With Scout installed and configured, add the Laravel\Scout\Searchable
trait to your Eloquent models to make them searchable. This trait will use Laravel’s model observers to keep the data in your model in sync with Meilisearch.
Here’s an example model:
To configure which fields to store in Meilisearch, use the toSearchableArray
method. You can use this technique to store a model and its relationships’ data in the same document.
The example below shows how to store a model’s relationships data in Meilisearch:
Configuring filterable and sortable attributes
Configure which attributes are filterable and sortable via your Meilisearch index settings.
In Laravel, you can configure your index settings via the config/scout.php
file:
The example above updates Meilisearch index settings for the Contact
model:
- it makes the
organization_id
field filterable - it makes the
name
andcompany_name
fields sortable
After changing your index settings, you will need to synchronize your Scout index settings.
Synchronizing your index settings
To synchronize your index settings, run the following command:
Example usage
You built an example application to demonstrate how to use Meilisearch with Laravel Scout. It showcases an app-wide search in a CRM (Customer Relationship Management) application.
This demo application uses the following features:
- Multi-search (search across multiple indexes)
- Multi-tenancy
- Filtering
- Sorting
Of course, the code is open-sourced on Github. 🎉
Was this page helpful?