Laravel Scout Documentation
repository·11.x·Indexed 23 days ago
https://github.com/laravel/scoutA driver-based solution for adding full-text search to Eloquent models, providing automatic synchronization of model changes to search indexes. Supports multiple search engines including Algolia, Meilisearch, Typesense, as well as Database, Collection, and Null drivers. Includes features for filtering results with constraints, sorting, pagination, and handling soft-deleted records.
What's inside Laravel Scout
- Laravel Scout is a driver-based solution for adding full-text search capabilities to your Eloquent models. It automates the synchronization of your model changes (such as creating, updating, or deleting records) with your search indexes, ensuring your search data remains consistent with your database.
Supported Search Drivers
11.xLaravel Scout currently supports the following search engines as drivers:
- Algolia
- Meilisearch
- Typesense
Algolia Driver: Update dependencies and handle exception changes (Scout 7.x)
11.xWhen using the Algolia driver with Scout 7.x, ensure you update your
algolia/algoliasearch-client-phpdependency to^2.2incomposer.json.Key changes in the Algolia client update:
- The
AlgoliaSearch\AlgoliaExceptionclass was renamed toAlgolia\AlgoliaSearch\Exceptions\AlgoliaException. - If passing a callback to the
searchmethod, the first argument is now an instance ofAlgolia/AlgoliaSearch/SearchIndex.
- The
Upgrade Meilisearch PHP SDK for Scout 10.x
11.xScout 10.x requires Meilisearch PHP 1.0 or higher. When upgrading, update your
composer.jsonto use^1.0.Important: In the 1.0 SDK, all namespace and class references changed from
MeiliSearch(capital S) toMeilisearch(lowercase s). You must update your code to reflect this change in capitalization."meilisearch/meilisearch-php": "^1.0"How MeilisearchEngine handles search queries and filters
11.xThe
MeilisearchEnginetranslates Scout'sBuilderqueries into Meilisearch-compatible parameters.Supported Filter Conversions:
- Standard
whereclauses: Converts operators like=,!=,>,<, etc., into Meilisearch filter syntax. - Booleans: Converts PHP booleans to Meilisearch
true/falseliterals. - Nulls: Converts
nullvalues toIS NULLorIS NOT NULLsyntax. - Enums: Supports
BackedEnumvalues by using their underlying value. whereInsandwhereNotIns: Converts these to MeilisearchINandNOT INsyntax using array brackets (e.g.,field IN [val1, val2]).
Sorting:
Scout
orderByclauses are converted to thecolumn:directionformat required by Meilisearch (e.g.,price:desc).- Standard
Handle Soft Deletes with Typesense
11.xIf your model uses theIlluminateoundation raits estores estores rait(Soft Deletes), theTypesenseEnginecan include soft delete metadata in the index. This is enabled if thescout.soft_deleteconfiguration option is set totrue.Configure Prefix Search using DatabaseEngine
11.xTo enable prefix searching (e.g., searching forterm%instead of%term%) within theDatabaseEngine, apply theSearchUsingPrefixattribute to the columns in your model'stoSearchableArraymethod. This instructs the engine to use aLIKEoperator with a trailing wildcard for those specific columns.Use the CollectionEngine for local searching
11.xThe
CollectionEngineis a search engine driver that uses local database collections for searching instead of an external service. It is useful as a fallback or for simple use cases where a full-text search engine like Algolia or Meilisearch is not required.When using this engine, Scout performs searches by querying the underlying Eloquent model and then filtering the results based on the
toSearchableArray()output of the models. It performs a case-insensitive partial match against the values returned bytoSearchableArray().Configure Full-Text Search using DatabaseEngine
11.xWhen using the
DatabaseEngine, you can leverage your database's native full-text search capabilities by applying theSearchUsingFullTextattribute to your model'stoSearchableArraymethod. This allows Scout to usewhereFullTextqueries instead of standardLIKEqueries for specific columns.Additionally, you can configure search options (like
languageormode) directly via the attribute arguments.Handle the new `wheres` property format in custom engines (Scout 11.x)
11.xIn Scout 11.x, the
wheresproperty on theBuilderinstance has changed from a simple key/value associative array to an array of arrays. Each entry in the array now containsfield,operator, andvaluekeys. This change enables support for comparison operators like>,<,>=,<=, and!=via thewheremethod.If you are implementing a custom engine and accessing the
wheresproperty directly, you must update your logic to iterate over the new structure.// New usage pattern for comparison operators User::search('*')->where('age', '>', 30)->get(); // How to access the property in a custom engine foreach ($builder->wheres as $where) { $field = $where['field']; $operator = $where['operator']; $value = $where['value']; // ... }Override `getScoutKeyName` with unqualified names (Scout 10.x)
11.xIn Scout 10.x, the
getScoutKeyNamemethod returns the unqualified key name and no longer qualifies it with the table name. If you are overriding this method in your models, ensure you return only the unqualified name (e.g.,'id'instead of'posts.id').Note: The
getUnqualifiedScoutKeyNamemethod was removed in 10.x as it is no longer necessary.public function getScoutKeyName() { return 'id'; }Configure Typesense search parameters in Scout
11.xYou can customize the search behavior for specific models by defining settings in your Scout configuration. The
TypesenseEnginelooks for model-specific settings under thescout.typesense.model-settingsconfiguration key.Supported configuration keys per model:
search-parameters.query_by: Defines which fields Typesense should search through.search-parameters.prefix: A boolean determining if prefix search is enabled (defaults totrue).collection-schema: The schema used to automatically create the Typesense collection when indexing objects.