Hello artisans, In this tutorial, we will learn how to do Full Text Search On Laravel. We are gonna use nicolaslopezj/searchable Package for full-text search. Searchable Package is a trait that adds a simple search function to Eloquent Models. For more information, you can click on this card bellow.

In this example, we will use full-text search to find posts by matching any of the columns title, content.

1- Install Searchable Package

First, we need to install nicolaslopezj/searchable Package in our project.

2- Create Post Model and Migration

Create Post model and migration by typing following command in cmd.

3- Add Attributes in Post Migration File

Add attributes in the “xxxx_xx_xx_xxxxxx_create_posts_table.php” file under the “database/migrations” directory.

Next, Migrate table by typing following command in cmd.

Read Also  Validate Custom Date Format with Laravel Validator


4- Modify Post Model

Add searchable Trait in Post Model

Now you can search in your model.


5- Search In Model

Custom Threshold

The default threshold for accepted relevance is the sum of all attribute relevance divided by 4. To change this value you can pass in a second parameter to search() like so:

Read Also  Laravel Model Events Tutorial

The above, will return all posts in order of relevance.

Entire Text search

By default, multi-word search terms are split and Searchable searches for each word individually. Relevance plays a role in prioritizing matches that matched on multiple words. If you want to prioritize matches that include the multi-word search (thus, without splitting into words) you can enable full text search by setting the third value to true. Example:

If you explicitly want to search for full text matches only, you can disable multi-word splitting by setting the fourth parameter to true.

I hope you have enjoyed this tutorial.

No Comments

Leave a Reply

your email address will not be published. required fields are marked *