Scout is a powerful full-text search package provided by Laravel that integrates with popular search engines like Algolia, Elasticsearch, and Meilisearch. It allows you to perform advanced search operations on your Eloquent models with ease. Here's a step-by-step guide on how to implement full-text search using Laravel Scout:
Step 1: Install Scout Package
composer require laravel/scout
Next, we have to publish our configuration file.
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"
Now we need to set configuration database as driver in your env file:
.env
SCOUT_DRIVER=database
Step 2: Update User Model
app/Models/User.php
<?php
namespace App\Models;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Laravel\Scout\Searchable;
class User extends Authenticatable
{
use HasApiTokens, HasFactory, Notifiable, Searchable;
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'name',
'email',
'password'
];
/**
* The attributes that should be hidden for serialization.
*
* @var array
*/
protected $hidden = [
'password',
'remember_token',
];
/**
* The attributes that should be cast.
*
* @var array
*/
protected $casts = [
'email_verified_at' => 'datetime',
];
/**
* Get the indexable data array for the model.
*
* @return array
*/
public function toSearchableArray()
{
return [
'name' => $this->name,
'email' => $this->email
];
}
}
Create Dummy Records:
php artisan tinker
User::factory()->count(20)->create()
Import Records:
php artisan scout:import "App\Models\User"
Step 3: Create Controller
app/Http/Controllers/UserController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\User;
use Illuminate\View\View;
class UserController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index(Request $request): View
{
if($request->filled('search')){
$users = User::search($request->search)->get();
}else{
$users = User::get();
}
return view('users', compact('users'));
}
}
Step 4: Add Route
routes/web.php
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('users', [UserController::class, 'index']);
Step 5: Create blade File
resources/views/users.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Laravel 10 Scout Full Text Search Tutorial - webthestuff.com</title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.0.1/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container">
<h1>Laravel 10 Scout Full Text Search Tutorial - webthestuff.com</h1>
<form method="GET">
<div class="input-group mb-3">
<input
type="text"
name="search"
value="{{ request()->get('search') }}"
class="form-control"
placeholder="Search..."
aria-label="Search"
aria-describedby="button-addon2">
<button class="btn btn-success" type="submit" id="button-addon2">Search</button>
</div>
</form>
<table class="table table-bordered data-table">
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
@foreach($users as $user)
<tr>
<td>{{ $user->id }}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</body>
</html>
Run Laravel Project:
php artisan serve
Now, Go to web browser, type the given URL and see the output:
http://localhost:8000/users