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:

User::factory(20)->create()

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.

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'])->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.

app/Http/Controllers/UserController.php

<?php

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()

->when(

$request->search,

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

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

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

}

)->paginate(5);

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:

resources/views/users.blade.php

<!DOCTYPE html>

<html>

<head>

<title>Laravel 10 Eloquent Model Search Example - ItSolutionStuff.com</title>

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

<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/5.0.1/css/bootstrap.min.css" rel="stylesheet">

</head>

<body>

<div class="container">

<div class="card">

<div class="card-header">

<h2>Laravel 10 Eloquent Model Search Example - ItSolutionStuff.com</h2>

</div>

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

<div class="col-auto">

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

</div>

</form>

<table class="table table-striped">

<tr>

<th>ID</th>

<th>Name</th>

<th>Email</th>

</tr>

@foreach ($users as $user)

<tr>

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

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

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

</tr>

@endforeach

</table>

{{ $users->links() }}

</div>

</div>

</div>

</body>

</html>

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:

http://localhost:8000/users

I hope it can help you...

Shares