1
votes

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.

1
Are you including all of the search parameters (as hidden inputs) in the form that includes the pagination buttons?Amarnasan

1 Answers

0
votes

From the docs:

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() }}

You are almost on the right track with this:

{{ $users->appends(array_filter(request()->except('page')))->render() }}

Wat this peace of code does is carry over the page-request when you switch pages, which is necessary since that is done for you. So you will need to specify the request data that you would like to keep. So for example:

{{ $users->appends(Request::except('page'))->links() }}

will add all request data to the next page, except the pagination links because that is added by default.

Note: i notice that you are sending id's with GET requests , please be carefull doing that. Users with bad intentions can see you database structure and possible take use of it.