How to keep filter data after the click to next button in Laravel pagination

On the internet, many developers face a problem with filter data after the click to next button in pagination Laravel 5.7

Here I explain how to filter data and use Laravel Pagination with filter data after the click to next button in pagination Laravel.


When using filter data and use pagination first page is shown perfectly with filter data change the page, the page changes to number to but we lose filtered data.

So, here is a solution.


Route::get('/', 'HomeController@search')->name('search');



 public function index(Request $request)
         $trainers = Trainer::orderBy('ranking', 'asc')->paginate(5);
         $data = $request->all();

        if ($request->has('expertise_area')) {
            $ids = $request->expertise_area;
            $trainers = Trainer::whereHas('expertiseAreas', function($q) use ($ids)
                $q->whereIn('expertise_area_id', $ids);
            })->orderBy('ranking', 'asc')->paginate(5);

        if ($request->has('state') && $request->has('city')) {
            $trainers = Trainer::where('state_id', $request->state)->where('city_id', $request->city)->orderBy('ranking', 'asc')->paginate(5);

        if ($request->has('state')) {
            $trainers = Trainer::where('state_id', $request->state)->orderBy('ranking', 'asc')->paginate(5);

        if ($request->has('city')) {
            $trainers = Trainer::where('city_id', $request->city)->orderBy('ranking', 'asc')->paginate(5);
        $states = State::all();
        $cities = City::all();
        $expertise_areas = ExpertiseArea::all();
        return view('welcome', compact('states', 'cities', 'expertise_areas', 'trainers','data'));



<div class="col-sm-4">
   <div class="search-box grey lighten-3">
      <h2>Search the Directory</h2>
      <h6 class="text-black">To search, you may select one or more of the categories below, and enter as many keywords in the search box. Then, just click the Search Button below. Keywords may include a name of a specific trainer, state or city.</h6>
      <div class="input-r">
         <!-- Default form contact -->
         <form action="{{ route('home') }}" method="get">
            <!-- State -->
            <h6 class="black-text mt-2 "><b class="category">Trainer Location (STATE)</b></h6>
            <select class="form-default browser-default custom-select mb-4" name="state" id="state">
               <option selected disabled>Choose State</option>
               @foreach ($states as $state)
               <option value="{{ $state->id }}">{{ $state->name }}</option>

            <h6 class="black-text mt-2 "><b class="category">Trainer Location (City)</b></h6>
            <!-- City -->
            <select class="browser-default custom-select mb-4" name="city" id="city">
               <option selected disabled>Choose City</option>
               @foreach ($cities as $city)
               <option value="{{ $city->id }}">{{ $city->name }}</option>
            <!-- Send button -->
            <button class="btn btn-info btn-block" type="submit">Search</button>
<div class="col-sm-8">
   <!--about profile-->
   @foreach ($trainers as $trainer)
   <div class="row pb-5">
      <div class="col-md-4">
         <div class="profiel-img">
            <img src="{{ asset($trainer->image) }}" alt="thumbnail" class="img-thumbnail" style="width: 200px">
         <a class="btn btn-primary btn-sm" style="font-size: 12px; margin-left: 40px;" href="{{ route('', $trainer) }}">Visit Profile</a>
      <div class="col-md-8 pb-3">
         <h3 class="black-text"> {{ $trainer->name }}</h3>
         <p class="black-text">{!! str_limit($trainer->about, 200) !!}</p>
            @foreach ($trainer->expertiseAreas as $area)
            <span class="badge badge-primary m-2 p-2">{{ $area->name }}</span>
         <hr style="border-top:1px solid #ccc;">
   <!--about profile ends-->
   {{ $trainers->appends($data)->links() }}



In the controller I use 

$data = $request->all();

To hold all request as the filter and pass this data to view

In View I receive data

{{ $trainers->appends($data)->links() }}

Now you can enjoy filter data after the click to next button in Laravel pagination.



About Author
  • user profile
    Brijpal Sharma, January 29th, 2018

    Hi, I am a web developer, I working on Laravel and CodeIgniter framework, I love to share knowledge and tutorials so I here...

0 Comment

You must be logged in to post a comment.

You may also be interested in...
jQuery plugin to validate the maximum length of the field and count characters.
Deploy Laravel Application To Shared Hosting The Easy and save Way
Laravel advance search filters with optional fields.
Laravel Migrations: Check if Table/Column Already Exists
Connect Multiple databases in the same Laravel project
SEO tool for Laravel Application
Paypal payment gateway integration in Laravel 5.7 step by step
How to Check User online or not in Laravel
How to keep filter data after the click to next button in Laravel pagination
Which is better for web development, Laravel or Flask?