In this tute, we will discuss laravel 9 scout full text search example. you can see laravel 9 scout search example. you will learn laravel 9 scout elasticsearch example. Here you will learn how to add full text search in laravel 9 scout. Let's get started with laravel 9 scout database engine example.
Laravel 9 provides Scout Package for full text search from your project. If you require to add full text search function in your laravel 9 application then you have to choose 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 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:
Here, we already have users table created, so we need use "Searchable" and create toSearchableArray() method. so let's update following code:
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 ]; }}
Next, we will create some dummy records on users table and import it.
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:
app/Http/Controllers/UserController.php
<?php 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) { if($request->filled('search')){ $users = User::search($request->search)->get(); }else{ $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.
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 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: