Laravel

 

How Laravel Scout Transforms Search in Eloquent Model-Based Apps

When it comes to web applications, one of the most sought-after features is a robust search capability. It can greatly enhance user experience and streamline data retrieval. Laravel, a popular PHP framework, offers a wonderful solution to this challenge with its package Laravel Scout.

How Laravel Scout Transforms Search in Eloquent Model-Based Apps

1. What is Laravel Scout?

Laravel Scout is an optional package that provides full-text search functionality for Eloquent models. With a simple configuration, you can enable blazing fast search capabilities on your application. Scout uses driver-based solutions, and out of the box, it supports Algolia, a cloud-based search service. However, developers can also implement custom drivers.

Let’s explore how you can integrate Laravel Scout into your project.

2. Installation and Configuration

To begin with, you’ll need to install Scout via Composer:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
```bash
composer require laravel/scout
```
```bash composer require laravel/scout ```
```bash
composer require laravel/scout
```

Once installed, you can publish its configuration:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
```bash
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
```
```bash php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider" ```
```bash
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
```

In your `.env` file, add any necessary Algolia configuration details:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
```bash
SCOUT_DRIVER=algolia
ALGOLIA_APP_ID=your_app_id
ALGOLIA_SECRET=your_secret_key
```
```bash SCOUT_DRIVER=algolia ALGOLIA_APP_ID=your_app_id ALGOLIA_SECRET=your_secret_key ```
```bash
SCOUT_DRIVER=algolia
ALGOLIA_APP_ID=your_app_id
ALGOLIA_SECRET=your_secret_key
```

However, if you’re opting for a different driver, you’ll configure it accordingly.

3. Making a Model Searchable

Once you’ve set up Scout, making an Eloquent model searchable is a breeze. Simply add the `Searchable` trait to your model.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
```php
use Laravel\Scout\Searchable;
class Post extends Model
{
use Searchable;
// Rest of your model code...
}
```
```php use Laravel\Scout\Searchable; class Post extends Model { use Searchable; // Rest of your model code... } ```
```php
use Laravel\Scout\Searchable;

class Post extends Model
{
    use Searchable;

    // Rest of your model code...
}
```

With this trait, your model will automatically keep an index of its records which can be used for searching.

4. Basic Search Functionality

For a basic search, Scout offers a straightforward approach. Here’s how you’d search for a post with a specific keyword:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
```php
$posts = App\Post::search('keyword')->get();
```
```php $posts = App\Post::search('keyword')->get(); ```
```php
$posts = App\Post::search('keyword')->get();
```

You’ll get a collection of `Post` instances matching the keyword.

5. Advanced Search

You can also make use of more advanced search capabilities, such as pagination:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
```php
$posts = App\Post::search('keyword')->paginate(10);
```
```php $posts = App\Post::search('keyword')->paginate(10); ```
```php
$posts = App\Post::search('keyword')->paginate(10);
```

You can also add conditions to the search:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
```php
$posts = App\Post::search('keyword')->where('published', 1)->get();
```
```php $posts = App\Post::search('keyword')->where('published', 1)->get(); ```
```php
$posts = App\Post::search('keyword')->where('published', 1)->get();
```

6. Customizing the Index

By default, all of the model’s columns are made searchable. However, sometimes you might want to limit the columns or modify how data is indexed. You can do so by defining a `toSearchableArray` method on the model:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
```php
public function toSearchableArray()
{
return [
'id' => $this->id,
'title' => $this->title,
'content' => $this->content,
];
}
```
```php public function toSearchableArray() { return [ 'id' => $this->id, 'title' => $this->title, 'content' => $this->content, ]; } ```
```php
public function toSearchableArray()
{
    return [
        'id' => $this->id,
        'title' => $this->title,
        'content' => $this->content,
    ];
}
```

This method determines the data that will be indexed. In the example above, only the `id`, `title`, and `content` fields of the `Post` model would be searchable.

7. Removing Records from Index

If, for any reason, you’d like to remove a record from the search index, you can do so easily:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
```php
$post->unsearchable();
```
```php $post->unsearchable(); ```
```php
$post->unsearchable();
```

To remove all records of a model from the index:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
```php
App\Post::unsearchable();
```
```php App\Post::unsearchable(); ```
```php
App\Post::unsearchable();
```

8. Importing Records

For larger databases, you might want to bulk import all records into your search index. This can be achieved with:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
```bash
php artisan scout:import "App\Post"
```
```bash php artisan scout:import "App\Post" ```
```bash
php artisan scout:import "App\Post"
```

Conclusion

Laravel Scout greatly simplifies the process of integrating full-text search capabilities into your Eloquent models. With driver-based solutions, you have the flexibility to opt for your preferred search engine or service. Its fluent API makes implementation a breeze, allowing you to focus on building amazing features for your application.

Whether you’re building a blog, an e-commerce site, or any other platform that requires search, Laravel Scout should be a top consideration for enhancing the user experience and providing rapid, accurate search results.

Previously at
Flag Argentina
Argentina
time icon
GMT-3
Experienced Full Stack Engineer with expertise in Laravel and AWS. 7 years of hands-on Laravel development, leading impactful projects and teams.