Meilisearch Integration in Laravel - Step by Step
Step 1: Install Laravel Scout
-----------------------------------
Install Laravel Scout via composer:
composer require laravel/scout
Step 2: Install Meilisearch PHP SDK
-----------------------------------
Install the official Meilisearch Laravel Scout driver:
composer require meilisearch/meilisearch-php
composer require meilisearch/scout-meilisearch-driver
Step 3: Configure .env file
-----------------------------------
Add these lines to your .env:
SCOUT_DRIVER=meilisearch
MEILISEARCH_HOST=http://127.0.0.1:7700
MEILISEARCH_KEY=your_meilisearch_master_key
Step 4: Publish Scout Config (optional)
-----------------------------------
Publish the configuration file:
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
Step 5: Set Up Your Model
-----------------------------------
Use the Searchable trait in your model (e.g., Product.php):
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;
class Product extends Model
{
use Searchable;
public function toSearchableArray()
{
return [
'id' => $this->id,
'name' => $this->name,
'description' => $this->description,
];
}
}
Step 6: Import Your Data into Meilisearch
-----------------------------------
Import existing records:
php artisan scout:import "App\\Models\\Product"
Step 7: Using the Search in Controller
-----------------------------------
Search example:
use App\Models\Product;
$products = Product::search('phone')->get();
Step 8: (Optional) Use Queue for Better Performance
-----------------------------------
Set in .env:
QUEUE_CONNECTION=database
SCOUT_QUEUE=true
Run the queue:
php artisan queue:work
Step 9: (Optional) Customize Search Settings
-----------------------------------
Customize search example:
Product::search('phone')
->within('products')
->orderBy('price', 'asc')
->get();
Quick Summary
-----------------------------------
| Step | What to Do |
|------|--------------------------------------|
| 1 | Install Laravel Scout |
| 2 | Install Meilisearch SDK |
| 3 | Configure `.env` settings |
| 4 | Publish Scout config |
| 5 | Add `Searchable` trait to your model |
| 6 | Import data to Meilisearch |
| 7 | Search using model search() method |
| 8 | (Optional) Use queue for indexing |
| 9 | (Optional) Customize search settings |
End of Guide