我正在使用带有post方法的表单,并且有多个复选框,当我选中一个复选框并提交表单时,它可以正常工作,并且结果带有分页,但是当我单击下一个分页链接时,它显示404页面错误找到。
查看:-
<form action="{{ route('trainer.filter') }}" method="post">
@csrf
<!-- Name -->
<input type="text" class="form-control mb-4" placeholder="Search" name="keywords" aria-label="Search">
<h6 class="black-text mt-2 "><b class="category">Areas of Experties</b></h6>
<!-- Copy -->
@foreach ($expertise_areas as $expertise_area)
<div class="custom-control custom-checkbox mb-1" style="padding-left: 3px;">
<label for="expertise_area">
<input type="checkbox" name="expertise_area[]" value="{{ $expertise_area->id }}"> {{ $expertise_area->name }}
</label>
</div>
@endforeach
<!-- 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>
分页链接:-
{{ $trainers->links() }}
路线:-
Route::post('trainer/filter', 'HomeController@trainerFilter')->name('trainer.filter');
控制器:-
public function trainerFilter(Request $request)
{
if ($request->has('keywords')) {
$trainers = Trainer::where('name', 'LIKE', '%'.$request->keywords.'%')
->orWhere('state_name', 'LIKE', '%'.$request->keywords.'%')
->orWhere('city_name', 'LIKE', '%'.$request->keywords.'%')->orderBy('ranking', 'asc')->paginate(5);
}}
答案 0 :(得分:1)
我认为您错过了通过结果集的不同页面在控制器内的位置中持久使用的参数 。
查看您发布的控制器代码,您可以尝试将keywords
参数附加到分页链接,如下所示:
{{ $trainers->appends(['keywords' => $keywords])->links() }}
请记住将['keywords' => $request->keywords]
传递到视图。