Harnessing the Power of Full-Text Search in Laravel

Harris Raftopoulos
2 min readOct 7, 2024

In the world of data retrieval, sometimes a simple LIKE clause just doesn't cut it. Enter full-text search - a powerful tool for efficient and relevant data queries. Laravel makes this advanced feature accessible through the whereFullText and orWhereFullText methods. Let's dive in!

Full-Text Search in Laravel

Laravel provides whereFullText and orWhereFullText methods to perform full-text searches on columns with full-text indexes. These methods are supported by MariaDB, MySQL, and PostgreSQL.

Basic Usage

Here’s a simple example of how to use whereFullText:

use Illuminate\Support\Facades\DB;

$users = DB::table('users')
->whereFullText('bio', 'web developer')
->get();

In this query, we’re searching for users whose ‘bio’ column contains the phrase ‘web developer’, utilizing the full-text capabilities of the database.

How It Works

Laravel intelligently transforms these methods into the appropriate SQL for your database system. For MariaDB and MySQL, it generates a MATCH AGAINST clause.

Key Points to Remember

  • Database Support: Currently works with MariaDB, MySQL, and PostgreSQL.
  • Full-Text Indexes: The columns you’re searching must have full-text indexes.
  • Natural Language Mode: By default, these methods use natural language mode for searching.

By leveraging full-text search capabilities in Laravel, you can significantly enhance the search functionality of your applications, providing faster and more relevant results to your users.

If this guide was helpful to you, subscribe to my daily newsletter and give me a follow on X/Twitter. It helps a lot!

--

--

Harris Raftopoulos
Harris Raftopoulos

Written by Harris Raftopoulos

Senior Software Engineer | 15+ years in PHP, 11+ with Laravel | Expert in Livewire, TailwindCSS & VueJS | DevOps Enthusiast | 🎤Speaker at Athens Laravel Meetup

No responses yet