Contact Us

New Package Laravel-Searchable: Easily Search in Multiple Models – Laravel Daily

Mobile App | July 31, 2020

Spatie team is still on fire with new packages. This week they released another one called Laravel Searchable, created mainly by AlexVanderbist. I’ve tried it myself and can show you a demo, along with my opinion.

What is 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.

Here’s an example search code from Controller:

Looks pretty simple and readable, right?

You would say there’s no need for another “search” package when we have Laravel Scout, Algolia, ElasticSearch and others, right? Here’s Freek Van der Herten’s official take on it:

laravel-searchable does not try to replace Scout. Both packages have their place. Make your own decision what you need in your project!

— Freek Van der Herten (@freekmurze)

Example mini-project: preparation

To test the package, I’ve created a fresh Laravel 5.7 project (the code will be available on GitHub – link at the end of article) with two database tables: categories and companies:

Also, seeded some data for both tables, used make:auth to generate a simple Bootstrap template, and ended up with this list of companies:

The code is really simple, here’s HomeController:

Now, let’s say we want to search both Categories and Companies from one text field. This is where the package will help us.

Notice a Search bar in top-right corner? Here’s HTML code for it:

Meanwhile, in routes/web.php, we have the homepage, search results, and pages for individual category/company:

So, “all” we need to do now, is implement HomeController@search method. Now, step-by-step.

Example mini-project: using Laravel Searchable

Step 1. Install the package.

That’s it, no more steps required at installation.

Step 2. Prepare the Models to be Searchable

This is where we need to do some manual work, in both app/Category.php and app/Company.php.

This is how Category model should look:

Let’s break it down, what changes have we made?

Really really similar transformations happen in app/Company.php:

Step 3. Performing the search from Controller

Here’s the code for our HomeController method:

As you can see, we’re creating a Search() object and registering TWO models with the fields that we need to search for. So that ‘name’ could be different for each model, it’s just a coincidence that it’s the same here.

Step 4. Viewing the Results: Grouped by Model

Here I’ve taken the example from the official documentation with a few small tweaks. Here’s our resources/views/search.blade.php:

And here’s how it looks:

As you can see, only one Company is returned but not the Category. Now, let’s try a query with both results present:

Here’s how $searchResults structure looks like if we do dd():

We’ve covered a basic usage, but there are a few more customizable things in Laravel Searchable. You can read them in the official documentation.

As promised, here’s the link to Github repository of this demo-project.

What do you think? Will you use the package?

Like our articles?
Check out our Laravel online courses!

This content was originally published here.