网页
综合
更新时间 2026-11-13 17:32:02
Laravel Scout | Laravel 13.x - The clean stack for Artisans and agents
自动蜘蛛
laravel.com
正文约 6000 字
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-in database engine that uses MySQL / PostgreSQL full-text indexes and LIKE clauses to search your existing database — no external service required. For most applications, this is all you need. For an overview of all search options available in Laravel, consult the search documentation.
Scout also includes drivers for Algolia, Meilisearch, Typesense, and Turbopuffer when you need features like typo tolerance, faceted filtering, vector search, or geo-search at massive scale. A "collection" driver is also available for local development, and you are free to write custom engines as well.
First, install Scout via the Composer package manager:
After installing Scout, you should publish the Scout configuration file using the vendor:publish Artisan command. This command will publish the scout.php configuration file to your application's config directory:
Finally, add the Laravel\Scout\Searchable trait to the model you would like to make searchable. This trait will register a model observer that will automatically keep the model in sync with your search driver:
When using an engine that is not the database or collection engine, you should strongly consider configuring a queue driver before using the library. Running a queue worker will allow Scout to queue all operations that sync your model information to your search indexes, providing much better response times for your application's web interface.
Once you have configured a queue driver, set the value of the queue option in your config/scout.php configuration file to true:
Even when the queue option is set to false, it's important to remember that some Scout drivers like Algolia and Meilisearch always index records asynchronously. In other words, even though the index operation has completed within your Laravel application, the search engine itself may not reflect the new and updated records immediately.
To specify the connection and queue that your Scout jobs utilize, you may define the queue configuration option as an array:
Of course, if you customize the connection and queue that Scout jobs utilize, you should run a queue worker to process jobs on that connection and queue:
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 the MakeSearchableUniquely and RemoveFromSearchUniquely job classes, typically within the boot method of a service provider:
These jobs use Laravel's unique job locks to avoid dispatching duplicate queued indexing operations for the same searchable model records while a matching job is already queued.
When using the Algolia driver, you should configure your Algolia id and secret credentials in your config/scout.php configuration file. Once your credentials have been configured, you will also need to install the Algolia PHP SDK via the Composer package manager:
Meilisearch is a fast, open source search engine that supports full-text, semantic, and hybrid search. If you aren't sure how to install Meilisearch on your local machine, you may use Laravel Sail, Laravel's officially supported Docker development environment. For production, you can self-host Meilisearch or use Meilisearch Cloud.
When using the Meilisearch driver you will need to install the Meilisearch PHP SDK via the Composer package manager:
Then, set the SCOUT_DRIVER environment variable as well as your Meilisearch host and key credentials within your application's .env file:
For more information regarding Meilisearch, please consult the Meilisearch documentation.
Typesense is a lightning-fast, open source search engine and supports keyword search, semantic search, geo search, and vector search.
You can self-host Typesense or use Typesense Cloud.
To get started using Typesense with Scout, install the Typesense PHP SDK via the Composer package manager:
Then, set the SCOUT_DRIVER environment variable as well as your Typesense host and API key credentials within your application's .env file:
If you are using Laravel Sail, you may need to adjust the TYPESENSE_HOST environment variable to match the Docker container name. You may also optionally specify your installation's port, path, and protocol:
Additional settings and schema definitions for your Typesense collections can be found within your application's config/scout.php configuration file. For more information regarding Typesense, please consult the Typesense documentation.
Turbopuffer is a search engine that supports full-text, semantic, and hybrid search. To use the Turbopuffer driver, set the SCOUT_DRIVER environment variable and provide your Turbopuffer API key:
The TURBOPUFFER_REGION environment variable is optional and defaults to gcp-us-central1.
By default, the entire toArray form of a given model will be persisted to its search index. If you would like to customize the data that is synchronized to the search index, you may override the toSearchableArray method on the model:
When searching, Scout will typically use the default search engine specified in your application's scout configuration file. However, the search engine for a particular model can be changed by overriding the searchableUsing method on the model:
The database engine currently supports MySQL and PostgreSQL, both of which provide support for fast, full-text column indexing.
The database engine uses MySQL / PostgreSQL full-text indexes and LIKE clauses to search your existing database directly. For many applications, this is the simplest and most practical way to add search — no external service or additional infrastructure required.
To use the database engine, set the SCOUT_DRIVER environment variable to database:
Once configured, you may define your searchable data and start execu
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。