Prerequisites
Before you start, make sure you have the following installed on your machine:- PHP
- Composer
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:artisan
command:
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:.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 ishttp://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.
Making Eloquent models searchable
With Scout installed and configured, add theLaravel\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:
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 theconfig/scout.php
file:
Contact
model:
- it makes the
organization_id
field filterable - it makes the
name
andcompany_name
fields sortable
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