Full-Text Search with MeiliSearch and Laravel Scout

Feature image: Full-Text Search with MeiliSearch and Laravel Scout

It feels so long ago that Laravel Scout was introduced: a great tool that lets you easily implement full-text search on Eloquent models. At launch, Laravel Scout included an Algolia driver–a service that provides hosted full-text search on their own servers–and options to make custom drivers. As with many hosted SaaS offerings, Algolia takes away a lot of headaches for developers and allows most teams to deploy quicker.

There are times, however, that I want to play with full-text search without going through Algolia. I wanted the simplicity of Algolia that I could host on my own, but without the complexities that I found in other self-hosted tools like Elasticsearch. I found this tool in MeiliSearch.

MeiliSearch is a simple self-hosted full-text search solution that's easy to integrate into a Laravel application–their team even maintains a Laravel Scout driver!

I'd like to show you how easily you can set up a locally-hosted full-text search server and connect it to your Laravel application. In this example, I will use a fresh install of Laravel 8, and will configure the App\Models\User model to be searchable via MeiliSearch/Scout.

Install Laravel Scout

composer require laravel/scout

Publish Scout configuration

php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"

Add the Laravel\Scout\Searchable trait to your model

namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
+use Laravel\Scout\Searchable;
class User extends Authenticatable
- use HasFactory, Notifiable;
+ use HasFactory, Notifiable, Searchable;

Install MeiliSearch Laravel Scout driver

composer require meilisearch/meilisearch-laravel-scout

Publish MeiliSearch configuration

php artisan vendor:publish --provider="Meilisearch\Scout\MeilisearchServiceProvider" --tag="config"

Update .env

If you do not have a key set, be sure to leave the empty MEILISEARCH_KEY= value in your .env file


Run MeiliSearch

To make implementation into your local development environment easier, Tighten has included MeiliSearch as a service in Takeout. I highly recommend (completely biased) installing Takeout and starting a MeiliSearch service. Otherwise, you can follow the installation instructions on the MeiliSearch Documentation.

If you decide to use Takeout, you can run MeiliSearch with:

takeout enable meilisearch

Create a search index

❯ php artisan scout:index users
Index "users" created.

Import documents to populate the index

❯ php artisan scout:import "App\Models\User"
Imported [App\Models\User] models up to ID: 10
All [App\Models\User] records have been imported.

Search your models!

❯ php artisan tinker
>>> App\Models\User::search('Rach')->get();
=> Illuminate\Database\Eloquent\Collection {#4219
all: [
App\Models\User {#4230
id: 5,
name: "Rachelle Anderson",
email: "jerod37@example.org",
email_verified_at: "2020-10-20 15:13:09",
created_at: "2020-10-20 15:13:09",
updated_at: "2020-10-20 15:13:09",

Keep going

The MeiliSearch documentation is comprehensive and covers a lot more use cases than this post. I recommend reading the documentation if you want to harness the full power of this great tool.

Stay in touch

I hope you come to enjoy working with MeiliSearch as much as I have. If you have feedback or would just like to say hi, follow me @JoseCanHelp. 👋

Get our latest insights in your inbox:

By submitting this form, you acknowledge our Privacy Notice.

Hey, let’s talk.

By submitting this form, you acknowledge our Privacy Notice.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Thank you!

We appreciate your interest. We will get right back to you.