Laravel 10 Eloquent Model Search Query Example

By Hardik Savani January 27, 2024 Category : Laravel

Hey Dev,

In this tutorial, you will discover laravel 10 search query. I explained simply step by step laravel 10 eloquent search query example. you will learn laravel eloquent search query. I’m going to show you about laravel search query using when condition.

In this tutorial, we'll create a simple example of how to use search query function in Laravel 10. we will create one simple route with GET request for display bootstrap form and users table data with input. Next, we will create UserController with one method: index(). In index() return layout and search function. just follow the following steps to create simple example.

Step 1 : Install Laravel Application

we are going from scratch, So we require to get fresh Laravel application using bellow command, So open your terminal OR command prompt and run bellow command:

composer create-project laravel/laravel example-app

Step 2: Create Dummy Users

First, we need to run migration command to create default "users" table. so, let's run the following command:

php artisan migrate

Now, we will run tinker command to create 20 dummy users. let's run the following commands:

php artisan tinker

then run the following command:


Step 3: Create Route

In this is step we need to create routes for users list with search form. so, update your web.php file.



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'])->name('users.index');

Step 4: Create Controller

In fourth step, we will create UserController with index() method. in index() method we will write search query code and return view. so, let's update code.



namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\User;

use Illuminate\Database\Eloquent\Builder;

class UserController extends Controller



* Display a listing of the resource.


* @return \Illuminate\Http\Response


public function index(Request $request)


$users = User::query()



function (Builder $builder) use ($request) {

$builder->where('name', 'like', "%{$request->search}%")

->orWhere('email', 'like', "%{$request->search}%");



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



Step 5: Create View

In Last step, let's create users.blade.php(resources/views/users.blade.php) for layout and lists all users with search form and put following code:


<!DOCTYPE html>



<title>Laravel 10 Eloquent Model Search Example -</title>

<meta name="csrf-token" content="{{ csrf_token() }}">

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



<div class="container">

<div class="card">

<div class="card-header">

<h2>Laravel 10 Eloquent Model Search Example -</h2>


<div class="card-body">

<form class="row g-3" method="GET" action="{{ route('users.index') }}">

<div class="col-auto">

<label for="search" class="visually-hidden">Search</label>

<input type="text" class="form-control" id="search" placeholder="Search" name="search"value="{{ request()->search }}">


<div class="col-auto">

<button type="submit" class="btn btn-primary mb-3">Search</button>



<table class="table table-striped">






@foreach ($users as $user)


<td>{{ $user->id }}</td>

<td>{{ $user->name }}</td>

<td>{{ $user->email }}</td>




{{ $users->links() }}






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:


I hope it can help you...