Laravel 9 Simple Search with Pagination Example
saim
Posted on August 9, 2022
In this we will see how to use simple search with pagination in laravel 9. Before we start you need to install laravel breeze start kit for ui.
Create Laravel Project
Run below command in your terminal to create laravel project.
composer create-project laravel/laravel laravel-search
Install laravel Breeze.
Laravel 9 Authentication with Laravel Breeze
Create User Fake Data
Laravel provider users fake data you need just uncomment seeder and run seeder.
database/seeders/DatabaseSeeder.php
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Seed the application's database.
*
* @return void
*/
public function run()
{
\App\Models\User::factory(20)->create();
// \App\Models\User::factory()->create([
// 'name' => 'Test User',
// 'email' => 'test@example.com',
// ]);
}
}
Run seeder with migration.
php artisan migrate:fresh --seed
Create User Controller
run below command to create user controller.
php artisan make:controller UserController
Add search functionality with pagination.
app/Http/UserController.php
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function index(Request $request)
{
$users = User::where([
['name', '!=', Null],
[function ($query) use ($request) {
if (($s = $request->s)) {
$query->orWhere('name', 'LIKE', '%' . $s . '%')
->orWhere('email', 'LIKE', '%' . $s . '%')
->get();
}
}]
])->paginate(6);
return view('users.index', compact('users'));
}
}
web.php
<?php
use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| 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('/', function () {
return view('welcome');
});
Route::get('/dashboard', function () {
return view('dashboard');
})->middleware(['auth'])->name('dashboard');
Route::get('users', [UserController::class,'index'])->name('users.search');
require __DIR__.'/auth.php';
Create User Blade file with search form.
views/users/index.blade.php
<x-app-layout>
<x-slot name="header">
<h2 class="text-xl font-semibold leading-tight text-gray-800">
{{ __('Users') }}
</h2>
</x-slot>
<div class="py-12">
<div class="mx-auto max-w-7xl sm:px-6 lg:px-8">
<div class="overflow-hidden bg-white shadow-sm sm:rounded-lg">
<div class="p-6 bg-white border-b border-gray-200">
<div class="mt-1 mb-4">
<div class="relative max-w-xs">
<form action="{{ route('users.search') }}" method="GET">
<label for="search" class="sr-only">
Search
</label>
<input type="text" name="s"
class="block w-full p-3 pl-10 text-sm border-gray-200 rounded-md focus:border-blue-500 focus:ring-blue-500 dark:bg-gray-800 dark:border-gray-700 dark:text-gray-400"
placeholder="Search..." />
<div class="absolute inset-y-0 left-0 flex items-center pl-4 pointer-events-none">
<svg xmlns="http://www.w3.org/2000/svg" class="w-5 h-5 text-gray-400" fill="none"
viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round"
d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" />
</svg>
</div>
</form>
</div>
</div>
<div class="relative overflow-x-auto shadow-md sm:rounded-lg">
<table class="w-full text-sm text-left text-gray-500 dark:text-gray-400">
<thead
class="text-xs text-gray-700 uppercase bg-gray-50 dark:bg-gray-700 dark:text-gray-400">
<tr>
<th scope="col" class="px-6 py-3">
#
</th>
<th scope="col" class="px-6 py-3">
Name
</th>
<th scope="col" class="px-6 py-3">
Email
</th>
</tr>
</thead>
<tbody>
@foreach ($users as $user)
<tr class="bg-white border-b dark:bg-gray-800 dark:border-gray-700">
<th scope="row"
class="px-6 py-4 font-medium text-gray-900 dark:text-white whitespace-nowrap">
{{$user->id}}
</th>
<td class="px-6 py-4">
{{$user->name}}
</td>
<td class="px-6 py-4">
{{$user->email}}
</td>
</tr>
@endforeach
</tbody>
</table>
{{ $users->links() }}
</div>
</div>
</div>
</div>
</div>
</x-app-layout>
💖 💪 🙅 🚩
saim
Posted on August 9, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.