The first page will be sorted correctly. However, the second page (and further) will go back to being as if no fields in the search were filled at all.
I am collecting the search fields in the form below:
{{ 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() }}
Once the form has been submitted it will hit a GET route
Route::get('members/search', 'UsersController@search')->name('admin.users.search');
Then the users controller:
$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'));
Then, in the view I am adding the pagination links like this:
{{ $users->appends(array_filter(request()->except('page')))->render() }}
However, the search results only work on the first page. for example, the route on the first page will look like this:
search?user_id=&email=hello%40world&first_name=John&family_name=Smith&institution_id=1&exam_id=1
But the second page will look like this:
search?page=2
I am finding this pretty puzzling and not too sure what is causing the search to fail on the second page.