Introduction
Laravel Scout provides a simple, driver-based solution for adding full-text search to your Eloquent models. Using model observers, Scout will automatically keep your search indexes in sync with your Eloquent records. Scout ships with a built-indatabase engine that uses MySQL / PostgreSQL full-text indexes and LIKE clauses to search your existing database — no external service required. For large-scale production workloads requiring typo tolerance, faceted filtering, or geo-search, external engines are available.
Supported engines
Installation
Install Scout via Composer:scout.php to your application’s config directory:
Laravel\Scout\Searchable trait to the model you want to make searchable. This trait registers a model observer that keeps the model in sync with your search driver:
Queueing
When using an engine other thandatabase or collection, you should strongly consider configuring a queue driver before using Scout. Running a queue worker allows Scout to queue all index sync operations in the background, providing much better response times for your web interface.
Set the queue option in config/scout.php to true:
Unique indexing jobs
In write-heavy applications, you may wish to prevent Scout from queueing duplicate jobs for the same model records. You may opt into unique indexing jobs by registering theMakeSearchableUniquely and RemoveFromSearchUniquely job classes, typically within the boot method of a service provider:
Driver prerequisites
Algolia
When using the Algolia driver, configure yourid and secret credentials in config/scout.php and install the Algolia PHP SDK:
.env file:
Index settings
You can manage Algolia index settings directly inconfig/scout.php:
Meilisearch
Meilisearch is a fast, open source search engine. For local development, the easiest way is to use Laravel Sail’s Docker environment..env file:
Index settings (Meilisearch)
Meilisearch requires you to pre-definefilterableAttributes for columns you plan to use with Scout’s where method, and sortableAttributes for columns you plan to sort by:
>, <, etc.) on data of the correct type:
Typesense
Typesense is a lightning-fast, open source search engine with support for keyword, semantic, geo, and vector search..env file:
toSearchableArray method must cast the model’s primary key to a string and creation date to a UNIX timestamp:
Database / collection engines
These built-in engines require no external service. The database engine uses MySQL / PostgreSQL full-text indexes andLIKE clauses. It is the recommended starting point for most applications:
The database engine searches your database tables directly — no separate indexing step is required.
The Searchable trait
Customizing toSearchableArray()
By default, the entiretoArray form of a model is persisted to its search index. Override toSearchableArray to control which data is synchronized:
Customizing the index name
By default, the model’s table name (plural) is used as the index name. OverridesearchableAs to customize it:
Database engine search strategies
For the database engine, you can assign PHP attributes to specify more efficient per-column search strategies:Conditionally searchable models
To make a model searchable only under certain conditions, define ashouldBeSearchable method:
Index management
The commands in this section are primarily relevant when using a third-party engine (Algolia, Meilisearch, or Typesense). The database engine does not require manual index management.
Batch import
If you are adding Scout to an existing project, import existing database records into your indexes:Flushing the index
Remove all records for a model from its search index:Pausing indexing
To perform a batch of Eloquent operations without syncing to the search index, usewithoutSyncingToSearch:
Manually adding and removing records
Add a collection of models to the index via an Eloquent query:unsearchable:
Searching
Use thesearch method to search a model. Chain get to retrieve the matching Eloquent models:
raw:
Pagination
Paginate search results with thepaginate method, which returns an Illuminate\Pagination\LengthAwarePaginator:
simplePaginate, which skips the total count query for better performance on large datasets:
Filtering and sorting
Add filter conditions to your search query withwhere:
query:
Eager loading
Scout retrieves IDs from the search engine and then fetches the models via Eloquent. To avoid N+1 queries, use thequery method to eager load relationships:
makeAllSearchableUsing on the model:
Soft deleting
If your indexed models use soft deletes and you need to search soft deleted records, setsoft_delete to true in config/scout.php:
withTrashed or onlyTrashed when searching:
Custom engines
If the built-in engines don’t meet your needs, you can write your own. Extend theLaravel\Scout\Engines\Engine abstract class and implement its eight required methods:
Laravel\Scout\Engines\AlgoliaEngine for a reference implementation.
Register your custom engine in the boot method of App\Providers\AppServiceProvider:
config/scout.php:
Related pages
Eloquent ORM
Learn the fundamentals of working with Eloquent models.
Eloquent relationships
Learn how to define relationships and use eager loading.
Queues
Scout can use queues to update indexes in the background.