$ lexprog.com

// notes from an old coder -- php, databases, and the occasional rant

[July 18, 2025] Laravel

Laravel Scout: Full-Text Search

Laravel Scout: Full-Text Search

────────────────────────────────────────────────────────

Laravel Scout: Full-Text Search

Tip: Make Model Searchable

use Searchable;

public function toSearchableArray(): array
{
    return ['id' => $this->id, 'title' => $this->title, 'content' => $this->content];
}

Gotcha: Sync Existing Records

php artisan scout:import "App\Models\Post"

Existing records aren't automatically indexed.

Tip: Search Query

$posts = Post::search('laravel tips')->get();

Gotcha: Conditional Searchable

public function shouldBeSearchable(): bool
{
    return $this->published;
}

Only published posts are indexed.

Tip: Database Driver for Development

'driver' => env('SCOUT_DRIVER', 'database'),

No external service needed for local development.

Gotcha: Searchable After Save

Models are automatically indexed on save/delete. Queue this for production: implements ShouldQueue.

Tip: Use route:cache Carefully

php artisan route:cache is fast, but it doesn't work with closure-based routes. Every time you cache routes, Laravel serializes them. If you have Route::redirect() or closure callbacks, the cache breaks. Stick to controller-based routes in production.

Tip: Model APP_KEY Rotation

Rotating APP_KEY invalidates all encrypted data — cookies, encrypted DB columns, and password reset tokens. If you must rotate (e.g., after a leak), plan a migration that re-encrypts existing data with the new key.

Gotcha: Local Scope Leaks

Global scopes defined in booted() apply to ALL queries on that model — including relationships. An innocent User::all() in admin panel might exclude soft-deleted users if a global scope is active.

Senior Insight

Full-text search in Laravel via Scout is convenient, but the default configuration can be misleading. I've seen apps where laravel/scout with the database driver was used in production, only to crash under load because it was running unoptimized LIKE queries. If you need real search, set up Meilisearch or Typesense from the start. Also, always configure Scout's queue integration — indexing synchronously in the request cycle destroys response times.

Source: Laravel News (https://laravel-news.com/), Freek.dev (https://freek.dev/tags/laravel), Spatie Blog (https://spatie.be/blog)

────────────────────────────────────────────────────────
<-- back to posts