
Laravel 9 pagination with search filter
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>
0You 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.
Random Code Snippet Queries: Laravel
- Get 30 days older records from table in laravel
- Route group with URI prefix using middleware and route name prefixes
- InRandomOrder() method with example in laravel
- Create model with migration and seeder
- Call to undefined method App\Models\User::follow()
- Create record with unique slug in laravel
- Laravel order by date not working
- Rendering HTML from database table to view in Laravel
- How to include header file in laravel
- Add [name] to fillable property to allow mass assignment on [App\Models\Project]
- How to get single column value in laravel
- Laravel get single row by id
- Get count of filter data, while return a small set of records
- Database transactions in laravel
- Add a subselect based on relationship using withAggregate method
- How to set column as primary key in Laravel model
- Get id of last inserted record in laravel
- Laravel get all records with pagination
- Array to string conversion laravel Controller
- How to run a specific seeder class in laravel
- Validation errors for multiple forms on same page Laravel
- Delete file from amazon s3 bucket using Laravel
- Call to undefined relationship [user] on model [App\Models\Post]
- The openssl extension is required for SSL/TLS protection but is not available
- Laravel URL validation not working