Laravel with Packages | Laravel 9 Scout Full-Text Search Tutorial

Step 1: Install Laravel 9

This is optional; however, if you have not created the laravel app, then you may go ahead and execute the below command:

composer create-project laravel/laravel example-app

Step 2: Install Scout

In this step, we have to install the scout package and we will publish the:

composer require laravel/scout
php artisan vendor:publish --provider="Laravel\Scout\ScoutServiceProvider"

Step 3: Update User Model

Here, we already have a user table created, so we need to use “Searchable” and create the toSearchableArray() method. so let’s update the following code:


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 = [

* The attributes that should be hidden for serialization.
* @var array
protected $hidden = [

* 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
php artisan tinkerUser::factory()->count(20)->create()
php artisan scout:import "App\Models\User"

Step 4: Create UserController Controller

At this point, now we should create a new controller as UserController. In this controller, we will add an index method, that will return users with a filter.


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;

class UserController extends Controller
* Display a listing of the resource.
* @return \Illuminate\Http\Response
public function index(Request $request)
$users = User::search($request->search)->get();
$users = User::get();

return view('users', compact('users'));

Step 5: Add Route

In this step, we need to create a route for listing users. so open your “routes/web.php” file and add the following route.

<?phpuse Illuminate\Support\Facades\Route;use App\Http\Controllers\UserController;Route::get('users', [UserController::class, 'index']);

Step 6: Create a View

In the Last step, let’s create users.blade.php(resources/views/users.blade.php) for layout and we will write design code here and put the following code:

<!DOCTYPE html>
<title>Laravel 9 Scout Full Text Search Tutorial - raviyatehnical</title>
<link href="" rel="stylesheet">

<div class="container">
<h1>Laravel 9 Scout Full Text Search Tutorial - raviyatehnical</h1>

<form method="GET">
<div class="input-group mb-3">
value="{{ request()->get('search') }}"
<button class="btn btn-success" type="submit" id="button-addon2">Search</button>

<table class="table table-bordered data-table">
@foreach($users as $user)
<td>{{ $user->id }}</td>
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>


php artisan serve



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store