How To Search In Multiple Models Using Laravel Searchable?

Laravel Searchable Tutorial: This week spatie has released a new package called Laravel Searchable which will give you customize the search in all models.

You can see a live demo on Laravel Searchable.

Here, you will learn about Laravel Searchable Package with an example after completing this tutorial.

What is Laravel Searchable?

A new package of Spatie makes

searching in multiple models

an easy task, without external dependencies. The main advantage, as I have tested it, is an ability to perform mega-search in all project database, specifying more than one model to search in.

#1: Create a New Project

Type the below command to generate a new Laravel project.

installing-laravel searchable project

#2: Installing Laravel Searchable Dependency

After creating a new project, Go to your project directory then install a Laravel Searchable package using composer.

#3: Configure Database

First, you need to create a database using phpMyAdmin or any other database tool then connect the created database in your .env file shown below.

#4: Creating a Migration and Model

I have created a migration and model for searching in multiple models with the help of laravel-searchable package. So, you can search for data in any models you want by implementing the Searchable interface.

Run the below command to generate an Article model and its migration.

Then, you will see an articles migration file inside database >> migrations directory. Open that file and add the below code in the up() method.

Once it is done, then run the migration by the following command.

#5: Creating a Seeder Class

Here, I am going to explain searching in multiple models using laravel-searchable which require more testing data. So, It can be possible with a seeder class than inserting manually in the database.

Therefore, I am going to create a seeder class to seed the database with test data.

It will generate a file called ArticleSeederClass inside database >> seeds directory. 

Now, Open the file and add the below code.

It will create 100 testing record in the article table after running db:seed Artisan command.

#6: Prepare The Models To Be Searchable

Searchable will search through a model. So first of all, you have to implement the searchable interface in a model which you want to make searchable.

I am going to make the Article model Searchable which created before.

Here, I want to search for article data. So, I have implemented the Searchable interface in the article model. I have also created getSearchResult() which will return the SearchResult object.

SearchResult() object will accept three parameters.

  • $this – where are you want to search?
  • $this->name – What is the returned column for the title.
  • $url – What URL the result should link?

#7: Creating a View

First of all, I am going to create a blade file inside resources >> views >> article.blade.php file with the input text box and search button. I have also fetched all the articles from the database in the same blade file.

#8: Create a Controller For Performing The Search

Then, I have created a controller called ArticleController for performing a search based on search terms in the registered models.

After that, It will create ArticleController file inside the app >> Http >> Controllers directory.

Go to that directory and open the file and replace with the below code.

Therefore, I have created two methods getArticles() which will fetch all the articles data from the database.

And, searchArticle() function which will perform on the user’s search query data in the registered model.

#9: Creating Routes For Views

In this tutorial, I have defined routes inside the routes >> web.php file to display the generated blade file’s contents through respective controllers.

#10: Create a View For Displaying Search Result

Now, I am going to create another blade file inside the resources >> views >> search-result.blade.php for displaying the search results.

It will display the search results of the user.


Laravel Searchable Tutorial

is over. I hope you have learned about laravel searchable and will implement in your next project. Thank you.

About the author

Siddharth Ghedia

Hi, my name is Siddharth. I'm from Rajkot, Gujarat, India. I'm passionate about Laravel. I have over 4 years of experience in Web Development. I work in the technologies like: Laravel, Vue, React, React Native, Angular, IONIC.

I've created this blog for sharing my knowledge with other developer who are willing to start and build their career in the technologies that I work in.


  • Hello i have use this package it’s to good and and easy to search from any tables and for custom search i have created custom search aspects.these all file in vendor so how to publish this folder because we not changes in vendor folder so how to publish….?

    Thanks & regards,

    • no need to worry about vendor directory. once you have done composer installation everywhere, then you will get “vendor” surely.


Leave a Comment