Passing GET Parameters When Using Laravel Pagination

Blade Eloquent Views

Committed to the LaraBrain by: wjgilmore (@wjgilmore) at June 23, 2016 7:22 pm

To paginate search results you'll need to tell Laravel to pass the search parameters from one page to the next

You can easily paginate database records in your database views by telling Laravel to paginate query results like so:

$users = User::where('active', true)->paginate();

With the collection passed into the view, you can iterate per usual and at the bottom of the output present a linked list of other available page numbers using the render() method:

{!! $users->render() !!}

But what if you additionally provided users with the ability to filter these results using a search field? You could modify the query like so:

$query = User::where('active', true);

if ($request->has('keyword')) {
    $query->where('email', 'LIKE', '%' . $request->get('keyword') . '%');
    $query->orWhereRaw('LOWER(email) LIKE ?', ['%' . strtolower($request->get('keyword')) . '%']);

$users = $query->paginate();

While this works, the linked page numbers won't automatically pass along the ?keyword=... string, meaning the filter won't be applied should the user navigate to another paginated page. Overriding this behavior is easily done using the appends() method:

{!! $users->appends(\Input::except('page'))->render() !!}

We're specifying the page parameter shouldn't be included because Laravel will already do that by default.

With this change in place, your paginated links will automatically include any additional parameters, like so:


Hope this helps!