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.
composer require laravel/scout
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
<?php 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;
composer require meilisearch/meilisearch-laravel-scout
php artisan vendor:publish --provider="Meilisearch\Scout\MeilisearchServiceProvider" --tag="config"
If you do not have a key set, be sure to leave the empty
MEILISEARCH_KEY=
value in your .env file
SCOUT_DRIVER=meilisearchMEILISEARCH_HOST=http://127.0.0.1:7700MEILISEARCH_KEY=
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
❯ php artisan scout:index usersIndex "users" created.
❯ php artisan scout:import "App\Models\User"Imported [App\Models\User] models up to ID: 10All [App\Models\User] records have been imported.
❯ 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", }, ], }
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.
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. 👋
We appreciate your interest.
We will get right back to you.