Laravel Search Multiple Models using Spatie Searchable Package

Hello Artisan

In this tutorial i will discuss about laravel multiple table search data with example code. I will show you haw we can search data from multiple table with search query. For doing that i will use spatie/laravel-searchable package. So from this tutorial you will also learn how you can use spatie/laravel-searchable package to search multiple model data from database.

Sometime we need to handle large scale application and that time we may need searching options from multiple model. In this situation we can do this easily using spatie/laravel-searchable package. 

So in this Laravel search multiple models example i will share source code with you so that you can understand. I will discuss step by step about Laravel search multiple tables. I will create Category and Product model and search data from those two model. So let's start our laravel search query for multiple table tutorial.

laravel-multiple-table-search-data-query

 

Step 1 : Create Routes

We need to routes one is for view form and another is for searching multiple table.

routes/web.php

oute::get('/', '[email protected]');
Route::get('/search', '[email protected]')->name('search');

 

Step 2 : Create Controller

We have to create TestController to add our two method. So create TestController and update it like below.

app/Http/Controllers/TestController.php

namespace App\Http\Controllers;

use App\Category;
use App\Product;
use Illuminate\Http\Request;  
use Spatie\Searchable\Search;

class TestController extends Controller
{
    public function index()
    {
      return view('welcome');
    }

    public function search(Request $request)
    {
       $searchResults = (new Search())
            ->registerModel(Product::class, 'name')
            ->registerModel(Category::class, 'name')
            ->perform($request->input('query'));

        return view('search', compact('searchResults'));
    }
}

 

Step 3 : Install spatie/laravel-searchable

Spatie’s package makes searching in models an easy task, without external dependencies.The main advantage, as I’ve tested it, is ability to perform mega-search in all project database, specifying more than one model to search in. 

Now in this step we have to install spatie/laravel-searchable package. So install it by the following command.

composer require spatie/laravel-searchable

 

Step 4 : Create Model

Now after installing this package we have to create two model. So create two model by the following command.

php artisan make:model Category -m
php artisan make:model Product -m

 

app/Category.php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Spatie\Searchable\Searchable;
use Spatie\Searchable\SearchResult;

class Category extends Model implements Searchable
{
   
    protected $fillable = [
        'name'
    ];

    public function getSearchResult(): SearchResult
    {
        $url = route('categories.show', $this->id);

        return new SearchResult(
            $this,
            $this->name,
            $url
         );
    }
}

 

app/Product.php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Spatie\Searchable\Searchable;
use Spatie\Searchable\SearchResult;

class Product extends Model implements Searchable
{
    protected $fillable = [
        'name', 'category_id'
    ];

    public function getSearchResult(): SearchResult
    {
        $url = route('product.show', $this->id);

        return new SearchResult(
            $this,
            $this->name,
            $url
        );
    }
}

 

Step 5 : Create Blade File

Now in this final step we have to create two blade file. One for form and another for showing search results. 

resources/views/welcome.blade.php

 

resources/views/search.blade.php

 

Read also : Laravel 7.x Ajax Autocomplete Search from Database

Now you can test it. Hope it can help you.