Laravel 12: How to Build Advanced Search with Laravel Scout

Laravel 12 How to Build Advanced Search with Laravel Scout

In today’s world, it is necessary that an application has the facility of quick and smart search in order to offer an optimal experience. This is because whatever type of website you develop, whether it is an e-commerce website, blogging platform, CRM, SaaS, etc., it must have the ability to display live search results and precise results.

By using Laravel Scout, implementing full-text search capabilities in your Laravel application has never been easier. It supports Laravel 12, which means you can now implement a simple yet powerful way to build advanced search engines using drivers of Algolia, Meilisearch, Typesense, and a database.

Laravel Development Services

In this blog post, we’ll show you how to implement an advanced searching feature using the Laravel Scout package in Laravel version 12.

Prerequisite:

1. Composer (latest Version)

2. Laravel version 12

Steps to Build Advanced Search with Laravel Scout:

Here are the steps to follow:

Step 1: Install Laravel 12

Step 2: Set Database Configuration and Migration

Step 3: Install Laravel Scout

Step 4: Create Model and Migration

Step 5: Configure Searchable Model

Step 6: Create Routes

Step 7: Create Controller

Step 8: Create Blade File

Step 9: Test Project

Now, let’s see all the steps with the detailed information.

Step  1: Install Laravel 12

For this demo, we need a new project. Create it using the following command:

composer create-project laravel/laravel:^12.0 scout-search-demo

Step 2: Set Database details and migrate

Set your database details in .env to your current database credentials, then run the following command to migrate your database.

php artisan migrate

Publish the Scout configuration file.

php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"

Step 3: Install Laravel Scout

Next, install the Laravel Scout package via the command shown below.

composer require laravel/scout

Set Scout driver in .env file

SCOUT_DRIVER=database

Step 4: Create Model and Migration

Next, create a Product model with migration.

php artisan make:model Product -m

Update migration file:

database/migrations/create_products_table.php

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
    public function up(): void
    {
        Schema::create('products', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->text('description')->nullable();
            $table->integer('price');
            $table->timestamps();
        });
    }
    public function down(): void
    {
        Schema::dropIfExists('products');
    }
};

Step 5: Configure Searchable Model

Next, update the Product model by adding the searchable attribute to it.

app/Models/Product.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;

class Product extends Model
{
    use Searchable;

    protected $fillable = [
        'name',
        'description',
        'price'
    ];

    /**
     * Get searchable array for the model
     */
    public function toSearchableArray(): array
    {
        return [
            'name' => $this->name,
            'description' => $this->description,
        ];
    }
}

Step 6: Create Routes

Next, create routes for product searches.

routes/web.php

<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProductController;
Route::get('/', [ProductController::class, 'index']);

Step 7: Create Controller

Next, we will create the ProductController using the command below.

php artisan make:controller ProductController

app/Http/Controllers/ProductController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Product;

class ProductController extends Controller
{
    /**
     * Display products with search
     */
    public function index(Request $request)
    {
        $search = $request->search;

        if ($search) {
            $products = Product::search($search)->get();
        } else {
            $products = Product::latest()->get();
        }

        return view('products', compact('products'));
    }
}

Step 8: Create Blade File

Next, create a blade file.

resources/views/products.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Laravel Scout Search</title>
</head>
<body>

<h2>Advanced Product Search</h2>

<form method="GET" action="/">
    <input type="text" name="search" placeholder="Search Products">
    <button type="submit">Search</button>
</form>

<br>

<table border="1" cellpadding="10">
    <tr>
        <th>ID</th>
        <th>Name</th>
        <th>Description</th>
        <th>Price</th>
    </tr>

    @foreach($products as $product)
    <tr>
        <td>{{ $product->id }}</td>
        <td>{{ $product->name }}</td>
        <td>{{ $product->description }}</td>
        <td>{{ $product->price }}</td>
    </tr>
    @endforeach

</table>

</body>
</html>

Step 9: Test Project 

Finally, test the project by executing the code below.

php artisan serve

Now, navigate to your web browser, input the provided URL, and check the project’s output:

http://localhost:8000/

Finally, use the search input field to search for the products, and Laravel Scout will display matching records from the database.

Conclusion

Using Laravel Scout, you can easily add search functionalities to your Laravel 12 applications and make use of an advanced search capability by integrating Meilisearch to ensure speedy filtering, ordering, pagination, and indexing of search results.

No matter whether you develop an eCommerce website, blog, or any other kind of application, Laravel Scout provides efficient and effective search functionalities with ease.

Hire Laravel Expert Now

FAQ

1. What is Laravel Scout?

Laravel Scout is a Laravel package having full-text search capability for Eloquent Models.

2. Which Search Engines Does Laravel Scout Work With?

Laravel Scout supports:

  • Meilisearch
  • Algolia
  • Typesense
  • Database driver

3. Does Laravel Scout support large datasets?

Yes, Laravel Scout is very effective in handling large data sets efficiently due to external search engine support.

Previous Article

Laravel 13: API Authentication using Sanctum Example

Next Article

Magento 2 Extensions Digest May 2026 (New Release & Updates)

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨