Laravel 10 Scout Full Text Search Tutorial

By Hardik Savani April 20, 2024 Category : Laravel

Hey Friends,

Today, I will let you know example of laravel 10 scout full text search example. you will learn laravel 10 scout search example. you'll learn laravel 10 scout elasticsearch example. if you want to see an example of how to add full text search in laravel 10 scout then you are in the right place.

Laravel 10 provides Scout Package for full text search from your project. If you require to add full text search function in your laravel 10 application then you have to choose the scout package to do.

In this example, we will install the scout composer package and use the database driver for full text search. then we will use scout with User model and add a name, email, and id column for full text search. let's see the below step to create full text search function.

Step for Laravel 10 Full Text Search using Scout Example

  • Step 1: Install Laravel 10
  • Step 2: Install Scout
  • Step 3: Update User Model
  • Step 4: Create UserController Controller
  • Step 5: Add Route
  • Step 6: Create View
  • Run Laravel App

Follow the below steps:

Step 1: Install Laravel 10

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 scout package and we will publish them:

composer require laravel/scout

Next, we have to publish our configuration file. so you need to run bellow command:

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

Now we need to set configuration database as driver in your env file:



Step 3: Update User Model

Here, we already have users table created, so we need use "Searchable" and create toSearchableArray() method. so let's update 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




Next, we will create some dummy records on users table and import it.

so let's run following commands:

Create Dummy Records:

php artisan tinker


Import Records:

php artisan scout:import "App\Models\User"

Step 4: Create UserController Controller

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

Let's update following code to your controller file:



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



$users = User::search($request->search)->get();


$users = User::get();


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



Step 5: Add Route

In this is step we need to create route for listing users. so open your "routes/web.php" file and add following route.



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 6: Create View

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


<!DOCTYPE html>



<title>Laravel 10 Scout Full Text Search Tutorial -</title>

<link href="" rel="stylesheet">



<div class="container">

<h1>Laravel 10 Scout Full Text Search Tutorial -</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>








Run Laravel App:

All the required steps have been done, now you have to type the given below command and hit enter to run the Laravel app:

php artisan serve

Now, Go to your web browser, type the given URL and view the app output:



Maybe it can help you.....