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 HomeController.php home.blade.php In the controller I use To hold all request as the filter and pass this data to view In View I receive data Now you can enjoy filter data after the click to next button in Laravel pagination.
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">
@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
$data = $request->all();
{{ $trainers->appends($data)->links() }}
About the author
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...
great answer !!!!
You must be logged in to post a comment.