A practical guide to search Eloquent relationships using Laravel 11 Scout Database Driver


Laravel Scout provides a simple and effective way to add full-text search functionality to your Eloquent models. By default, Scout supports several search engines like Algolia and MeiliSearch. However, using a database driver for Scout allows you to leverage your existing database infrastructure for search functionality. Here’s a practical guide on how to search Eloquent relationships using Laravel Scout with the database driver in Laravel 11.

Step-by-Step Guide

1. Install Laravel Scout and Database Driver

First, you need to install Laravel Scout and the Scout database driver package:


composer require laravel/scout composer require scoutapp/scout-apm-laravel

2. Publish Scout Configuration

Next, publish the Scout configuration file:


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

This will create a config/scout.php file where you can configure Scout.

3. Configure Scout

In the config/scout.php file, set the driver to database:


return [ // Other configurations... 'driver' => env('SCOUT_DRIVER', 'database'), // Other configurations... ];

4. Add the Searchable Trait to Your Model

To make an Eloquent model searchable, use the Searchable trait. For example, if you have a Post model:


// app/Models/Post.php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Laravel\Scout\Searchable; class Post extends Model { use HasFactory, Searchable; /** * Get the indexable data array for the model. * * @return array */ public function toSearchableArray() { return $this->toArray(); } /** * The relationships that should be eagerly loaded on every query. * * @var array */ protected $with = ['comments']; }

If your Post model has a relationship with a Comment model, define the relationship in your model:


public function comments() { return $this->hasMany(Comment::class); }

5. Make Related Models Searchable

If you want to include related models in the search, you should also add the Searchable trait to those models. For example:


// app/Models/Comment.php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Laravel\Scout\Searchable; class Comment extends Model { use HasFactory, Searchable; /** * Get the indexable data array for the model. * * @return array */ public function toSearchableArray() { return $this->toArray(); } }

6. Define Indexing for Relationships

To include related models' data in the search index, you can customize the toSearchableArray method in your main model (Post):


public function toSearchableArray() { $array = $this->toArray(); // Include related comments in the searchable data $array['comments'] = $this->comments->map(function ($comment) { return $comment->only(['id', 'body']); }); return $array; }

7. Index Your Models

After configuring the searchable models, you need to import your data into the search index:


php artisan scout:import "App\Models\Post" php artisan scout:import "App\Models\Comment"

8. Performing Searches

You can now perform searches on your models. For example, to search for posts containing a specific term:


$posts = Post::search('keyword')->get();

This will return a collection of Post models that match the search criteria.

Example Search with Relationships

If you want to search for posts based on comments as well, ensure your toSearchableArray method includes the related data, as shown above. Here’s how you might perform such a search:


$posts = Post::search('comment text or post keyword')->get();

Conclusion

By following these steps, you can leverage Laravel Scout with the database driver to perform full-text searches across your Eloquent models and their relationships. This approach provides a simple yet powerful way to add search functionality to your Laravel 11 application without requiring an external search engine.


Post a Comment

0 Comments