Laravel 11 Eloquent Model Search Query Example

Hello, laravel web developers! In this article, we'll see the laravel 11 eloquent model search query. Here, we'll see the laravel eloquent search relationship. we'll show you about laravel search query using the when the condition in laravel 11.

In this tutorial, we'll create a simple example of how to use the search query function in Laravel 11. we'll create a simple route for displaying the bootstrap form and users table.

Laravel 11 Eloquent Model Search Query

laravel 11 eloquent model search query


Step 1: Install the Laravel 11 Application

In this step, we'll install the laravel 11 application using the following command.

composer create-project laravel/laravel laravel-11-application


Step 2: Create Dummy Users

Next, we'll create some dummy users using the following command.

php artisan migrate

Now, open the tinker using the following command.

php artisan tinker



Step 3: Define Route

Then, we'll define the route into the 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

Next, we'll create a UserController to define the index() function and add the search logic into that function.


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

Then, we'll create a users.blade.php file and add the following HTML code to that file.


<!DOCTYPE html>
    <title>Laravel 11 Eloquent Model Search Query Example - Techsolutionstuff</title>
    <meta name="csrf-token" content="{{ csrf_token() }}">
    <link href="" rel="stylesheet">
<div class="container">
    <div class="card">
      <div class="card-header">
        <h2>Laravel 11 Eloquent Model Search Query Example - Techsolutionstuff</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() }}
Step 6: Run the Laravel 11 Application

Now, run the laravel 11 application using the following command.

php artisan serve


You might also like:


Techsolutionstuff | The Complete Guide

I'm a software engineer and the founder of Hailing from India, I craft articles, tutorials, tricks, and tips to aid developers. Explore Laravel, PHP, MySQL, jQuery, Bootstrap, Node.js, Vue.js, and AngularJS in our tech stack.