Skip to main content

Black Friday 2025! Only until December 1st: coupon FRIDAY25 for 40% off Yearly/Lifetime membership!

Read more here

Laravel Search by Query/Tag With Auto Complete - Laravel Scout and Alpine.js

This project demonstrates how to add search with suggestions using Laravel Scout and Alpine.js.

How It Works

For this example, a database scout driver is used.

// ...
 
SCOUT_DRIVER=database

The Searchable trait must be added to the Eloquent models from which search you will want to do. A good practice would be to add searchable fields to the toSearchableArray() method as an array in the model.

app/Models/Category.php:

use Laravel\Scout\Searchable;
use Illuminate\Database\Eloquent\Model;
 
class Category extends Model
{
use Searchable;
 
// ...
 
public function toSearchableArray(): array
{
return [
'id' => $this->id,
'name' => $this->name,
];
}
}

app/Models/Product.php:

use Laravel\Scout\Searchable;
use Illuminate\Database\Eloquent\Model;
 
class Product extends Model
{
use Searchable;
 
// ...
 
public function toSearchableArray(): array
{
return [
'id' => $this->id,
'title' => $this->title,
'code' => $this->code,
];
}
}

Next, we will:

  • Set up the Search Blade component
  • Set up the Search Controller

Want to Get Access to GitHub Repository?

Unlock the complete README, installation instructions, code walkthrough, and direct access to clone the repository. Join our premium membership to access all project examples.

Full Source Code
Clone and customize
Documentation
Complete setup guides
All Examples
15 premium projects
Become a Premium Member for $129/year or $29/month

We'd Love Your Feedback

Tell us what you like or what we can improve

Feel free to share anything you like or dislike about this page or the platform in general.