DEV Community

Cover image for Laravel 9 Simple Search with Pagination Example

Posted on • Originally published at

Laravel 9 Simple Search with Pagination Example

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 
Enter fullscreen mode Exit fullscreen mode

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.


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()->create([
        //     'name' => 'Test User',
        //     'email' => '',
        // ]);
Enter fullscreen mode Exit fullscreen mode

Run seeder with migration.

php artisan migrate:fresh --seed
Enter fullscreen mode Exit fullscreen mode

Create User Controller

run below command to create user controller.

php artisan make:controller UserController
Enter fullscreen mode Exit fullscreen mode

Add search functionality with pagination.


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

        return view('users.index', compact('users'));
Enter fullscreen mode Exit fullscreen mode



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');
Route::get('users', [UserController::class,'index'])->name('');
require __DIR__.'/auth.php';
Enter fullscreen mode Exit fullscreen mode

Create User Blade file with search form.


    <x-slot name="header">
        <h2 class="text-xl font-semibold leading-tight text-gray-800">
            {{ __('Users') }}

    <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('') }}" method="GET">
                                <label for="search" class="sr-only">
                                <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="" 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" />

                    <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">
                                class="text-xs text-gray-700 uppercase bg-gray-50 dark:bg-gray-700 dark:text-gray-400">
                                    <th scope="col" class="px-6 py-3">
                                    <th scope="col" class="px-6 py-3">
                                    <th scope="col" class="px-6 py-3">
                                @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">
                                    <td class="px-6 py-4">

                                    <td class="px-6 py-4">

                        {{ $users->links() }}

Enter fullscreen mode Exit fullscreen mode

laravel 9 search with pagination

Top comments (0)