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.

Problem:

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.

 web.php

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

 

HomeController.php

 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'));
    }

 

home.blade.php

<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">
            @csrf
           
            
            <!-- 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>
               @endforeach
            </select>

            <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>
               @endforeach
            </select>
            <!-- Send button -->
            <button class="btn btn-info btn-block" type="submit">Search</button>
         </form>
      </div>
   </div>
</div>
<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">
         </div>
         <a class="btn btn-primary btn-sm" style="font-size: 12px; margin-left: 40px;" href="{{ route('trainer.show', $trainer) }}">Visit Profile</a>
      </div>
      <div class="col-md-8 pb-3">
         <h3 class="black-text"> {{ $trainer->name }}</h3>
         <p class="black-text">{!! str_limit($trainer->about, 200) !!}</p>
         <h5>
            @foreach ($trainer->expertiseAreas as $area)
            <span class="badge badge-primary m-2 p-2">{{ $area->name }}</span>
            @endforeach
         </h5>
         <hr style="border-top:1px solid #ccc;">
      </div>
   </div>
   <!--about profile ends-->
   @endforeach
   {{ $trainers->appends($data)->links() }}
</div>

 

notice

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

    brijpalsharma99@gmail.com, January 29th, 2018

    Hello, My Name is Brijpal Sharma. I am a Web Developer, Professional Blogger and Digital Marketer from India. I am the founder of Codermen. I started this blog to help web developers & bloggers by providing easy and best tutorials, articles and offers for web developers and bloggers...


0 Comment


You must be logged in to post a comment.