Learn how to set up Laravel Scout in Laravel 11 with this detailed guide. Explore the installation process, configuration options, and search driver setup to boost your application’s search functionality.
In today’s web applications, having effective search features is key to improving user experience. Laravel Scout is a handy tool that works with Laravel 11 to offer full-text search capabilities. This guide will walk you through the process of setting up Laravel Scout, configuring it with Meilisearch, and refining your search setup with advanced techniques.
Step 1: Install Laravel Scout
To begin, make sure you have Laravel installed on your system. You can add Laravel Scout to your project using Composer. Open your terminal and run the following command:
composer require laravel/scout
This command will add Laravel Scout to your project, allowing you to integrate it with your Laravel application.
Step 2: Configure Scout
After the installation is finished, you’ll need to publish the Scout configuration file to customize its settings according to your needs.
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
This will generate a config/scout.php file where you can customize the default settings to fit your requirements.
Step 3: Choose a Search Driver
Laravel Scout works with different search engines, with Algolia being the default option. To use a different search driver, simply update the config/scout.php file.
'driver' => env('SCOUT_DRIVER', 'algolia'),
For this tutorial, we will use Meilisearch. Update your .env file to set:
SCOUT_DRIVER=meilisearch
Step 4: Set Up Meilisearch
If you opt for Meilisearch, follow the official Meilisearch documentation to install and configure it. After installation, configure Meilisearch in your .env file:
MEILISEARCH_HOST=http://127.0.0.1:7700
Ensure you have set SCOUT_DRIVER=meilisearch to use Meilisearch as your search engine.
Step 5: Add the Searchable Trait to Your Model
To enable search functionality for an Eloquent model, use the Searchable trait. For example, modify your Post model as follows:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
class Post extends Model
{
use Searchable;
// Additional model code...
}
Adding this trait allows Laravel Scout to index and search the Post model’s records.
Step 6: Index Your Data
Populate your search index by running the scout:import Artisan command:
php artisan scout:import "App\Models\Post"
This command syncs all existing records of the Post model with your search engine, making them searchable.
Step 7: Performing Searches
With indexing complete, you can perform searches on your model. Here’s how to search for posts by a keyword:
$posts = App\Models\Post::search('keyword')->get();
This query retrieves all posts that match the specified keyword.
Step 8: Customizing Search Behavior
To customize how models are indexed, override the toSearchableArray method in your model:
class Post extends Model
{
use Searchable;
public function toSearchableArray()
{
$array = $this->toArray();
// Customize array...
return $array;
}
}
This method allows you to tailor the data indexed by Scout.
Step 9: Handling Indexing in Background
To handle indexing in the background, use Laravel’s queue system. Enable this by setting SCOUT_QUEUE to true in your .env file:
SCOUT_QUEUE=true
Ensure your queue worker is running with:
php artisan queue:work
This setup enhances performance by handling indexing tasks in the background.
Step 10: Testing Your Search Implementation
Make sure your search feature performs correctly by writing tests. Here’s a simple example using PHPUnit:
use Illuminate\Foundation\Testing\RefreshDatabase;
use App\Models\Post;
class SearchTest extends TestCase
{
use RefreshDatabase;
public function test_search_returns_expected_results()
{
Post::factory()->create(['title' => 'Laravel Scout Tutorial']);
Post::factory()->create(['title' => 'Another Post']);
$results = Post::search('Laravel')->get();
$this->assertCount(1, $results);
}
}
Testing helps verify that your search setup works properly and delivers the results you expect.
For More detail you can refer this Laravel Scout Document
Summary
By following this comprehensive guide, you should now have a fully functional Laravel Scout setup in your Laravel 11 project. From installation to advanced usage, Laravel Scout enhances your application’s search capabilities, making it easier for users to find relevant information quickly and efficiently. Explore additional features and tailor Scout to fit your application’s specific needs.
You may also find interesting:
Laravel Scout in Laravel 11: A Comprehensive Tutorial with Example