laravel pagination with search: I am going to use Search Functionality with Advance Pagination within the CRUD (Create Table, Select, Insert, Update, Delete Table) table. Also Click for the CRUD (Create Table, Select, Insert, Update, Delete Table) Functionality TABLE.
Q : How to use Search Functionality with Pagination in Laravel PHP Framework?
laravel pagination with search
Information search like Categories, User, Member, Products, Contact in website using Laravel 8, Today, We are share best example simple pagination for search results in Laravel 8 app’s.
laravel pagination with search filter
if $id has some value it will include main conditions like as a “where(‘id’,’<',$id) else will return all"
$products = DB::table('products') ->when($id, function ($query, $id) { return $query->where('id','<',$id); }) ->paginate(20);
Okay, Now you can install laravel 8 latest version
Create Project for laravel pagination with search
composer create-project --prefer-dist laravel/laravel blog "8.0.*"
After then, you need make controller , you run command the following
# Make Controller in Laravel
php artisan make:controller FindController --resource
FindController.php file in directory App/Http/Controller, you open file and pass the following PHP Source code
public function index(Request $request) { $search = $request->input('q'); if($search!=""){ $Members = Member::where(function ($query) use ($search){ $query->where('name', 'like', '%'.$search.'%') ->orWhere('email', 'like', '%'.$search.'%'); }) ->paginate(2); $Members->appends(['q' => $search]); } else{ $Members = Member::paginate(2); } return View('pages.search')->with('data',$Members); // }
If Member don’t search or find, you get data all Member in Member table
if Member search, you required using $search = $request->input(‘q’); get search data value, and then append youe some query string keyword $Members->appends([‘q’ => $search]);
Ok, you to resources/views/pages directory create search.blade.php file, aften then you pass the following code
Pagination View : Now in the view file search.blade.php.
@extends('layouts.app') @section('content')@endsection@foreach($data as $Member)
{{ $data->links() }}@endforeach {{$Member->ID}} {{$Member->name}} {{$Member->email}} {{$Member->created_at}} {{$Member->updated_at}}
you can simply Configuration Route in laravel web application, open web.php in routes/web.php folder
Route::get("/search",'FindController@index');
Run Paginated data with Search functionality in Laravel Table
php artisan serve
http://localhost:8000/search