Laravel Full Text Search Tutorial

If you are looking for a Laravel full text search, this tutorial might be helpful for you. In order to make full-text search, you need to add a column index called FullText.

Imagine that we are going to use model User as a full-text search. So, we are planning following field to be searched as a full text. Just alter your table-

ALTER TABLE users ADD FULLTEXT fulltext_index (username, email, bio, details)

 

Now in your user.php model, define the searchable array-

Create Full Text Search Trait.

Create the file FullTextSearch.php in the same folder as your models and paste the following code.

Add Your trait

Now add your trait to your model.

Start Searching

For multiple tables, you need to follow this-

  • Add the Full-Text index
  • Run the migration
  • Define the searchable columns
  • Import the FullTextSearch trait

 

 

This tip is fully inspired by arianacosta