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
In this step, we'll install the laravel 11 application using the following command.
composer create-project laravel/laravel laravel-11-application
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
User::factory(20)->create()
Then, we'll define the route into the 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');
Next, we'll create a UserController to define the index() function and add the search logic into that function.
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'));
}
}
Then, we'll create a users.blade.php file and add the following HTML code to that file.
resources/views/users.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Laravel 11 Eloquent Model Search Query Example - Techsolutionstuff</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 11 Eloquent Model Search Query Example - Techsolutionstuff</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>
Now, run the laravel 11 application using the following command.
php artisan serve
You might also like: