On my desk

Global Search Using Laravel and Livewire

I have implemented a global search feature on this portfolio website. For a quick practical demonstration, click on the search icon in the navigation bar and type anything. If you want to know how I did it, check this out! This portfolio website is built using Laravel and Livewire. To get started, you should first have a Laravel application installed. Once you have your basic app setup ready, follow these steps to implement the global search feature.

By Published Updated

Working log

Step 1: Install Laravel Scout and Livewire

CODE SAMPLE Code snippet
composer require laravel/scout livewire/livewire

Step 2: Publish Scout Configuration

This will create scout.php configuration file in config directory.

CODE SAMPLE Code snippet
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"

Step 3: Set Scout Configuration in .env

If you want to enable database search for your application, update your .env file to set the Scout driver to database. Laravel offers other search options as well, which you can explore in the Laravel documentation. I have implemented Mysql database search.

CODE SAMPLE Code snippet
SCOUT_DRIVER=database

Step 4: Add Searchable Trait and toSearchableArray to Model

You can repeat this to multiple models as needed. In my case, I have a Tip model, so I added these to the Tip model.

CODE SAMPLE Code snippet
use Laravel\Scout\Searchable; // Import Scout's Searchable trait

class Tip extends Model
{
    use Searchable; // Use Searchable trait provided by Scout for this model

        protected $fillable = [
        'title',
        'description',
    ];

    // Define model fields you want to search on (important step)
    public function toSearchableArray(): array
    {
        return [
            'title' => $this->title,
            'description' => $this->description,
        ];
    }
}

Step 5: Create a Livewire Component

Generate a Livewire component for the search functionality.

CODE SAMPLE Code snippet
php artisan make:livewire GlobalSearch

Step 6: Implement the Livewire Component

In app/Http/Livewire/GlobalSearch.php

CODE SAMPLE Code snippet
namespace App\Livewire;

use App\Models\Tip;
use Livewire\Component;

class GlobalSearch extends Component
{
    public $query;
    public $suggestions = [];
    public $show_suggestions = false;

    public function updatedQuery()
    {
        $this->suggestions = [];
        $this->show_suggestions = false;

        if (!empty($this->query)) {
            $this->suggestions = Tip::search($this->query)->get();
            if( !empty($this->suggestions) ) $this->show_suggestions = true;
        }
    }

    public function render()
    {
        return view('livewire.global-search');
    }
}

Step 7: Update Livewire Components View File

In resources/views/livewire/global-search.blade.php

CODE SAMPLE Code snippet
<div>
    <input type="text" wire:model.live="query" placeholder="Search Tips ...">

    @if($results && $show_suggestions)
        <ul>
            @foreach($suggestions as $suggestion)
                <li>
                    {{ $suggestion->title }}
                </li>
            @endforeach
        </ul>
    @endif
</div>

Step 8: Add the Livewire Component to Your View

Include the Livewire component in your main view. I have added to my main layout file. (resources/views/layouts/app.blade.php)

CODE SAMPLE Code snippet
@livewire('global-search')

This way you can implenet a simple global search feature in your Laravel application that searches through your MySQL database.

To extend this feature to other models, simply repeat 'Step 4' for each model. In my case, I've implemented this for Tip and Blog models.