搜索后分页仅适用于第一页
Pagination after search only works on the first page
第一页将正确排序。但是,第二页(以及更远的页面)将恢复为好像搜索中根本没有填写任何字段一样。
我正在收集以下表格中的搜索字段:
{{ Form::open(['route' => 'admin.users.search', 'method' => 'get', 'class' => 'navbar-form navbar-left form-inline', 'role' => 'search']) }}
<div class="form-group">
{{ Form::text('user_id', request('user_id'), ['class' => 'form-control', 'size' => '8', 'placeholder' => 'ID']) }}
</div>
<div class="form-group">
{{ Form::email('email', request('email'), ['class' => 'form-control', 'size' => '20', 'placeholder' => 'Email']) }}
</div>
<div class="form-group">
{{ Form::text('first_name', request('first_name'), ['class' => 'form-control', 'size' => '20', 'placeholder' => 'First Name']) }}
</div>
<div class="form-group">
{{ Form::text('family_name', request('family_name'), ['class' => 'form-control', 'size' => '20', 'placeholder' => 'Family Name']) }}
</div>
<div class="form-group">
<div class="selectize-lg">
{{ Form::select('institution_id', $institutions, request('institution_id'), ['class' => 'form-control', 'size' => '200', 'data-selectize']) }}
</div>
</div>
<div class="form-group">
<div class="selectize-lg">
{{ Form::select('exam_id', $exams, request('exam_id'), ['class' => 'form-control', 'data-selectize']) }}
</div>
</div>
<div class="form-group ">
{{ Form::submit('Search', ['class' => 'btn btn-default']) }}
</div>
<a href="{{ route('admin.users.index') }}" class="btn btn-warning">Clear</a>
{{ Form::close() }}
提交表单后,它会点击 GET 路由
Route::get('members/search', 'UsersController@search')->name('admin.users.search');
然后是用户控制器:
$users = User::with('exam', 'institution');
if ($request->has('user_id')) {
$users->whereId($request->user_id);
}
if ($request->has('email')) {
$users->whereEmail($request->email);
}
if ($request->has('first_name')) {
$users->where('first_name', 'LIKE', "%{$request->first_name}%");
}
if ($request->has('family_name')) {
$users->where('family_name', 'LIKE', "%{$request->family_name}%");
}
if ($request->has('institution_id')) {
$users->whereInstitutionId($request->institution_id);
}
if ($request->has('exam_id')) {
$users->whereExamId($request->exam_id);
}
$users = $users->latest()->paginate(48);
$usersTotal = $users->total();
$exams = ['' => 'Exam...'] + Exam::orderBy('title')
->pluck('title', 'id')
->all();
$institutions = ['' => 'University...'] + Institution::orderBy('name')
->pluck('name', 'id')
->all();
return view('admin.users.index', compact('users', 'usersTotal', 'exams', 'institutions'));
然后,我在视图中添加这样的分页链接:
{{ $users->appends(array_filter(request()->except('page')))->render() }}
但是,搜索结果只在第一页有效。例如,第一页上的路线将如下所示:
search?user_id=&email=hello%40world&first_name=John&family_name=Smith&institution_id=1&exam_id=1
但是第二页会是这样的:
search?page=2
我觉得这很费解,也不太确定是什么原因导致第二页上的搜索失败。
Appending To Pagination Links
You may append to the query string of pagination links using the
appends method. For example, to append sort=votes to each pagination
link, you should make the following call to appends:
{{ $users->appends(['sort' => 'votes'])->links() }}
If you wish to
append a "hash fragment" to the paginator's URLs, you may use the
fragment method. For example, to append #foo to the end of each
pagination link, make the following call to the fragment method:
{{ $users->fragment('foo')->links() }}
你几乎走在了正确的轨道上:
{{ $users->appends(array_filter(request()->except('page')))->render() }}
当您切换页面时,这段代码的和平之处在于继承页面请求,这是必要的,因为这是为您完成的。因此,您需要指定要保留的请求数据。例如:
{{ $users->appends(Request::except('page'))->links() }}
会将所有请求数据添加到下一页,但分页链接除外,因为这是默认添加的。
注意:我注意到您正在使用 GET 请求发送 ID,请小心操作。恶意用户可以看到您的数据库结构并可能利用它。
第一页将正确排序。但是,第二页(以及更远的页面)将恢复为好像搜索中根本没有填写任何字段一样。
我正在收集以下表格中的搜索字段:
{{ Form::open(['route' => 'admin.users.search', 'method' => 'get', 'class' => 'navbar-form navbar-left form-inline', 'role' => 'search']) }}
<div class="form-group">
{{ Form::text('user_id', request('user_id'), ['class' => 'form-control', 'size' => '8', 'placeholder' => 'ID']) }}
</div>
<div class="form-group">
{{ Form::email('email', request('email'), ['class' => 'form-control', 'size' => '20', 'placeholder' => 'Email']) }}
</div>
<div class="form-group">
{{ Form::text('first_name', request('first_name'), ['class' => 'form-control', 'size' => '20', 'placeholder' => 'First Name']) }}
</div>
<div class="form-group">
{{ Form::text('family_name', request('family_name'), ['class' => 'form-control', 'size' => '20', 'placeholder' => 'Family Name']) }}
</div>
<div class="form-group">
<div class="selectize-lg">
{{ Form::select('institution_id', $institutions, request('institution_id'), ['class' => 'form-control', 'size' => '200', 'data-selectize']) }}
</div>
</div>
<div class="form-group">
<div class="selectize-lg">
{{ Form::select('exam_id', $exams, request('exam_id'), ['class' => 'form-control', 'data-selectize']) }}
</div>
</div>
<div class="form-group ">
{{ Form::submit('Search', ['class' => 'btn btn-default']) }}
</div>
<a href="{{ route('admin.users.index') }}" class="btn btn-warning">Clear</a>
{{ Form::close() }}
提交表单后,它会点击 GET 路由
Route::get('members/search', 'UsersController@search')->name('admin.users.search');
然后是用户控制器:
$users = User::with('exam', 'institution');
if ($request->has('user_id')) {
$users->whereId($request->user_id);
}
if ($request->has('email')) {
$users->whereEmail($request->email);
}
if ($request->has('first_name')) {
$users->where('first_name', 'LIKE', "%{$request->first_name}%");
}
if ($request->has('family_name')) {
$users->where('family_name', 'LIKE', "%{$request->family_name}%");
}
if ($request->has('institution_id')) {
$users->whereInstitutionId($request->institution_id);
}
if ($request->has('exam_id')) {
$users->whereExamId($request->exam_id);
}
$users = $users->latest()->paginate(48);
$usersTotal = $users->total();
$exams = ['' => 'Exam...'] + Exam::orderBy('title')
->pluck('title', 'id')
->all();
$institutions = ['' => 'University...'] + Institution::orderBy('name')
->pluck('name', 'id')
->all();
return view('admin.users.index', compact('users', 'usersTotal', 'exams', 'institutions'));
然后,我在视图中添加这样的分页链接:
{{ $users->appends(array_filter(request()->except('page')))->render() }}
但是,搜索结果只在第一页有效。例如,第一页上的路线将如下所示:
search?user_id=&email=hello%40world&first_name=John&family_name=Smith&institution_id=1&exam_id=1
但是第二页会是这样的:
search?page=2
我觉得这很费解,也不太确定是什么原因导致第二页上的搜索失败。
Appending To Pagination Links
You may append to the query string of pagination links using the appends method. For example, to append sort=votes to each pagination link, you should make the following call to appends:
{{ $users->appends(['sort' => 'votes'])->links() }}
If you wish to append a "hash fragment" to the paginator's URLs, you may use the fragment method. For example, to append #foo to the end of each pagination link, make the following call to the fragment method:
{{ $users->fragment('foo')->links() }}
你几乎走在了正确的轨道上:
{{ $users->appends(array_filter(request()->except('page')))->render() }}
当您切换页面时,这段代码的和平之处在于继承页面请求,这是必要的,因为这是为您完成的。因此,您需要指定要保留的请求数据。例如:
{{ $users->appends(Request::except('page'))->links() }}
会将所有请求数据添加到下一页,但分页链接除外,因为这是默认添加的。
注意:我注意到您正在使用 GET 请求发送 ID,请小心操作。恶意用户可以看到您的数据库结构并可能利用它。