Posts Learn Components Snippets Categories Tags Tools new About
/

Make Your Laravel Search Smartarter With Laravel Scout and MeiliSearch

Learn how to implement Laravel Scout with (MeiliSearch driver) to make your search smarter

4 months ago

10 mins read

1864 views

In this post, you'll learn how to integrate Laravel Scout with (MeiliSearch driver) to enhance and make your search smarter by getting more accurate results, typo tolerant, blazing-fast queries, and more.
MeiliSearch Homepage

What is MeiliSearch?


So a little bit of MeiliSearch, it's the "Next-generation search API" which is just like Algolia but it's open-source and free to use. By default, MeiliSearch is blazing fast, tolerant to typos, synonymous, comes with filterings, language support, and more.
Now that you know a little bit of MeiliSearch, let's get started and learn how to implement it to your Laravel project and see for yourself how powerful and easy it's to have a powerful search engine.

Intro: Dummy Data


For the sake of this tutorial, imagine you have an "Post" model with dummy data generated by the Laravel factory (fakerphp). This model will be the one marked as Searchable so that it can be integrated with Laravel Scout.

Post table structure:

  • id
  • title
  • content

Step 1: Install Laravel Scout


Within your Laravel project, install Laravel Scout using composer.
composer require laravel/scout

Step 2: Install MeiliSearch PHP Driver


Besides Laravel Scout, you will also have to install the MeiliSearch driver and its necessary dependencies.
composer require meilisearch/meilisearch-php http-interop/http-factory-guzzle

Step 3: Publish Scout Configuration


Once it's installed, publish the Scout configuration as follows. The output configuration will be located inside "config/scout.php".
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"

Step 4: Update .env Configuration File


The next step is to add the environment configuration for Laravel Scout. Do specify it as follows.
SCOUT_DRIVER=meilisearch
MEILISEARCH_HOST=http://127.0.0.1:7700
MEILISEARCH_KEY=

Step 5: Add Searchable Trait to Your Model


From the model instance, import the "Searchable" trait which will allow the model to be queryable.
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;

class Post extends Model
{
    use Searchable;

    /* other model codes */
}

Step 6: Install MeiliSearch on Your Platform


Do note that you need to have MeiliSearch installed on your machine. If you are using macOS then you can refer to this Installing MeiliSearch on macOS Big Sur. Otherwise if you are on the other platforms, then you can refer to MeiliSearch documentation for that.
brew update && brew install meilisearch

Step 7: Launch MeiliSearch


To launch/run MeiliSearch, you can type in "meilisearch" on your terminal, and once it's ready visit "http://127.0.0.1:7700/" which will contain the dashboard of your MeiliSearch data. Do note that it's empty by default since there are no data being imported yet.
meilisearch

Step 8: Import Data to MeiliSearch


To import the associated data you can run "scout:import" command and you can specify the models as the command arguments "App\Models\Post".
php artisan scout:import "App\Models\Post"

Step 9: View Data from MeiliSearch Dashboard


If you want to see the data that have been imported to MeiliSearch, just go ahead and visit localhost with port 7700.
http://127.0.0.1:7700/
MeiliSearch Dashboard

Step 10: Search Model


Now to search the "Post" model instance you can call the "search" method available to the model. Once you get the result you can take X number of posts then retrieve it as a collection.
$query = "Learn Laravel";

$posts = Post::search($query)->take(10)->get();
The code above shows a simple way to query the "Post" model but for a real-world use case example, if you want to put more constraint on the columns to query or load relation, you can define it like below.
// routes/web.php

use Illuminate\Http\Request;

Route::get('/search-post', function (Request $request) {
  $query = $request->query('query');

  $posts = Post::search($query)->take(10)->get();

  return view('posts.index', compact('posts'));
});
Run it on your localhost or local domain name.
php artisan serve

Now when you visit the browser you can type in "/search-post?query=Learn Laravel" then it will trigger the route and the search result would be returned as a collection.
http://localhost:8000/search-post?query=Learn Laravel
By now you should be able to set up and implement Laravel Nova with MeiliSearch driver, on the next post you'll learn how to implement the front-end UI with Laravel Livewire. So make sure to bookmark PostSrc and stay tuned for the next post. If you found this tutorial to be helpful, do share it with your friends, and happy coding. 🎉

Resources

Alternative Tags

If you like our tutorial, do make sure to support us by being our Patreon or buy us some coffee ☕️

new

PostSrc Code Snippets

Learn new snippets today, level up your Laravel, Alpine JS, Vue JS, Tailwind CSS skills and more.

Learn New Snippets
new

PostSrc Code Components

Collection of Tailwind CSS components for everyone to use. Browse all of the components that are right for your project.

View Components
)