
Laravel 9 pagination with search filter
Laravel 9 pagination with search filter
Good day, guys. In this post, we’ll look at how to solve the "Laravel 9 pagination with search filter" programming puzzle.
Sometimes we have to filter data from a table in Laravel using the search keyword provided by users. Here we provide search functionality with pagination in Laravel 9.-
Pagination with search filter in Laravel 9
//Run below command to make HomeController
php artisan make:controller HomeController
routes\web.php
use App\Http\Controllers\HomeController; Route::any('/home', [HomeController::class, 'index'])->name('user');
app\Http\Controllers\HomeController.php
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\User; class HomeController extends Controller { public function index(Request $request){ $users = User::paginate(2); $search = $request->q; if($search!=""){ $users = User::where(function ($query) use ($search){ $query->where('name', 'like', '%'.$search.'%') ->orWhere('email', 'like', '%'.$search.'%'); }) ->paginate(2); $users->appends(['q' => $search]); } else{ $users = User::paginate(2); } return view('home', compact('users')); } }
resources\views\home.blade.php
<form action="{{ route('user') }}" method="POST"> @csrf <input type="text" name="q" placeholder="search by user name and email..." /> <button type="submit"> Search </button> </form> <table class="table table-bordered"> <thead> <tr> <th>First Name</th> <th>Email</th> </tr> </thead> <tbody> @foreach($users as $user) <tr> <td>{{ $user->name }}</td> <td>{{ $user->email }}</td> </tr> @endforeach </tbody> </table> <div> {{ $users->links() }} </div>
You have to create a controller using php artisan make:controller HomeController.
Define route in web.php
Create controller method in HomeController class.
You also have to create a blade file home.blade.php in resources/views directory.
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- How to insert dynamic value to additional column in pivot table in laravel
- Fatal error: Composer detected issues in your platform: Your Composer dependencies require a PHP version ">= 8.0.0"
- Remove array keys and values if it does not exist in other array in Laravel
- Display message with session flash using bootstrap alert class in laravel
- If no route matched route::fallback in laravel
- How to decrypt laravel password
- Laravel API response format
- How to Access Array in blade laravel
- How to insert multiple rows in mysql using loop in laravel?
- SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key
- How to create and run user seeder in laravel
- Target class [App\Http\Controllers\Auth\Request] does not exist.
- RuntimeException You must enable the openssl extension in your php.ini to load information from https://repo.packagist.org
- How to create new user without form submission in laravel
- Split an Eloquent Collection by half in Laravel
- SQLSTATE[42S02]: Base table or view not found: 1146 Table 'laravel8.projects' doesn't exist
- Get products with number of orders in Laravel
- Ajax GET request in laravel
- How to pass query string with pagination in laravel
- How to get images from AWS s3 and display in Laravel blade
- How to check query string exists or not in laravel blade
- How to upload multiple images after preview in laravel using cropper js
- Validation errors for multiple forms on same page Laravel
- How to insert ckeditor data into database in Laravel?
- Get count of filter data, while return a small set of records