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 >
  <div >
   <h2>Search the Directory</h2>
   <h6 >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 >
     <!-- Default form contact -->
     <form action="{{ route('home') }}">
      @csrf
      
      
      <!-- State -->
      <h6 ><b >Trainer Location (STATE)</b></h6>
      <select name=state id="state">
        <option selected disabled>Choose State</option>
        @foreach ($states as $state)
        <option value="{{ $state->id }}">{{ $state->name }}</option>
        @endforeach
      </select>

      <h6 ><b >Trainer Location (City)</b></h6>
      <!-- City -->
      <select 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 type=submit>Search</button>
     </form>
   </div>
  </div>
</div>
<div >
  <!--about profile-->
  @foreach ($trainers as $trainer)
  <div >
   <div >
     <div >
      <img src="{{ asset($trainer->image) }}" alt="thumbnail" >
     </div>
     <a  href="{{ route('trainer.show', $trainer) }}">Visit Profile</a>
   </div>
   <div >
     <h3 > {{ $trainer->name }}</h3>
     <p >{!! str_limit($trainer->about, 200) !!}</p>
     <h5>
      @foreach ($trainer->expertiseAreas as $area)
      <span >{{ $area->name }}</span>
      @endforeach
     </h5>
     <hr >
   </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.