Using Laravel Scout for Full-Text Search in Laravel Applications
This guide explains how to install, configure, and use Laravel Scout to add powerful full-text search capabilities to Laravel models, covering driver setup, searchable traits, indexing, custom queries, conditional searching, pagination, and result display.
Why Full-Text Search?
Full-text search improves user experience by returning results that match part or all of the search terms, making it essential for applications such as online stores, music services, or content‑rich websites.
Setting Up Laravel Scout
Install Laravel Scout via Composer and publish its configuration file.
composer require laravel/scout
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"Configuring Laravel Scout
Select a driver (e.g., Algolia, Meilisearch, Typesense) and set the SCOUT_DRIVER environment variable in config/scout.php .
'driver' => env('SCOUT_DRIVER', 'algolia'),
'algolia' => [
'id' => env('ALGOLIA_APP_ID', ''),
'secret' => env('ALGOLIA_SECRET', ''),
],Making Eloquent Models Searchable
Add the Laravel\Scout\Searchable trait to any model and define the toSearchableArray method.
use Laravel\Scout\Searchable;
class YourModel extends Model
{
use Searchable;
public function toSearchableArray()
{
return [
'id' => $this->id,
'title' => $this->title,
// add other searchable attributes
];
}
}Indexing and Updating Records
When a model is created or saved, Scout automatically indexes it; updates are also synced. You can import existing records with:
php artisan scout:import "App\Models\YourModel"Custom Import Queries
Define a makeAllSearchableUsing method on the model to customize the query used for bulk imports, such as eager‑loading relationships.
Removing Records from the Index
Deleting a model automatically removes it from the index, or you can call the unsearchable method on a collection of models.
Pausing Indexing
Wrap Eloquent operations with withoutSyncingToSearchScout to prevent immediate synchronization with the search index.
Conditional Searchable Models
Implement a shouldBeSearchable method to index only models that meet specific conditions, such as a published status.
Performing Searches with Laravel Scout
After indexing, execute searches with optional filters, sorting, and pagination.
$results = YourModel::search('Search Query')->get();Result Pagination
Scout integrates with Laravel's paginator for easy result paging.
$results = YourModel::search('Search Query')->paginate(10);Displaying Search Results
Iterate over the result collection in a Blade view to show desired attributes.
@foreach($results as $result)
{{ $result->title }}
{{-- Display other attributes --}}
@endforeachConclusion
Laravel Scout provides a powerful, easy‑to‑use full‑text search solution for Laravel applications, simplifying indexing and querying to enhance user experience and application interactivity.
php中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.