Hello, web developer! In this article, we'll see laravel 10 eloquent model search query. In this tutorial, you'll learn how to implement a search query in Laravel 10. I'll walk you through a simple, step-by-step example using Laravel's Eloquent search query. You'll also see how to use the when
condition to enhance your search functionality.
We'll create a basic example that includes a GET route to display a Bootstrap form and a users table with a search input. Then, we'll set up a UserController
with an index()
method that handles the layout and search function. Follow these steps to build a straightforward search query example in Laravel 10.
Laravel 10 Eloquent Model Search Query
In this step, we'll install laravel 10 application using the following command.
composer create-project laravel/laravel laravel-11-example
Then, we'll run migration command to create default "users" table.
php artisan migrate
Now, we;ll run tinker command to create some dummy users. let's run the following commands.
php artisan tinker
then run the following command.
User::factory(50)->create()
Then, we'll define the routes 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 controller and add the following code to that file.
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 blade file and add the following HTML code to that file.
resources/views/users.blade.php
<!DOCTYPE html>
<html>
<head>
<title>Laravel 10 Eloquent Model Search Query - Vidvatek</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 Query - Vidvatek</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 10 application using the following command.
php artisan serve
You might also like:
- Read Also: How to Install jQuery in Laravel 10 Vite
- Read Also: Laravel 10 Custom Forgot Password
- Read Also: Laravel 10 Create Form Request Validation
- Read Also: Laravel 10 Filter Datatable with Dropdown