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.
Related Queries
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
- SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.post_id' in 'where clause
- Composer create project laravel/laravel example app
- Automatically remove records using Prunable trait in Laravel
- How to get file extension from input type file in laravel
- How to return a column with different name in Laravel
- Setup laravel project with docker
- Redirect to previous page or url in laravel
- Undefined property: stdClass::$title
- How to display a specific word from a string in laravel
- How to get date from created_at field in laravel
- How to add script on specific view file in laravel while extending layout
- Laravel 10 starter app using breeze on live server
- SQLSTATE[23000]: Integrity constraint violation: 1022 Can't write; duplicate key in table
- There are no commands defined in the "route:" namespace
- Send OTP using textlocal api in laravel
- Pagination in laravel
- Shorter syntax for whereHas with call back function in laravel
- Class 'Facade\Ignition\IgnitionServiceProvider' not found
- Input file with max size validation in laravel
- Fatal error: Composer detected issues in your platform: Your Composer dependencies require a PHP version ">= 8.0.0"
- How to call Laravel route in jQuery
- How to get random string in Laravel
- Syntax error or access violation: 1072 Key column 'role_id' doesn't exist in table (SQL: alter table `users` add constraint `users_role_id_foreign` foreign key (`role_id`) references `roles` (`id`))
- Target class [admin] does not exist.
- Trying to access array offset on value of type null error in laravel