laravel pagination with search Filter Best Example

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"

Read Also:  Laravel Eloquent Get Last Inserted Id

$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

Read Also:  Generate Bacon-qr code generator laravel Example

Pagination View : Now in the view file search.blade.php.

@extends('layouts.app')
@section('content')
<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-10">
            <form action="">
                <h2>Search content in database using Laravel</h2>
                <div class="form-group">
                    <input type="text" name="q" placeholder="Find or Search Members...!" class="form-control"/>
                    <input type="submit" class="btn btn-primary" value="Search"/>
                </div>
            </form>
        </div>
        <div class="col-md-10">
            <table class="table">
                @foreach($data as $Member)
                <tr>
                    <td>{{$Member->ID}}</td>
                    <td>{{$Member->name}}</td>
                    <td>{{$Member->email}}</td>
                    <td>{{$Member->created_at}}</td>
                    <td>{{$Member->updated_at}}</td>

                </tr>
                @endforeach
            </table>
            {{ $data->links() }}
        </div>
    </div>
</div>
@endsection

you can simply Configuration Route in laravel web application, open web.php in routes/web.php folder

Read Also:  jquery Onmouseover Onmouseout show/hide loop

Route::get("/search",'[email protected]index');

Run Paginated data with Search functionality in Laravel Table

php artisan serve

http://localhost:8000/search

Pagination for search results laravel
Pagination for search results laravel