Laravel Full Text Search

It’s often required to make a full-text search in your table. However, the concept is not that easy to understand. In this tip, I will show you how to do search full-text in laravel for 1 or more than 1 column.

Database table setup:

Need to change table structure a bit in order to implement full-text.

ALTER TABLE articles ADD FULLTEXT(body);

Query:

In general, we can write a code to search data based on any specific keyword. That’s fine. However, our aim to search data and return a result based on most equivalent results.

In raw PHP-MySq

This query brings records based on search string from the database. Now in Laravel, you can write this query-

This one also brings the same result. Nice.

What if you need to get results based on matching score in a descending way? Well, you can do that too.

Obviously, you can apply more logic here.