'Laravel Pagination links not including other GET parameters

I am using Eloquent together with Laravel 4's Pagination class.

Problem: When there are some GET parameters in the URL, eg: http://site.com/users?gender=female&body=hot, the pagination links produced only contain the page parameter and nothing else.

Blade Template

{{ $users->link() }}

There's a ->append() function for this, but when we don't know how many of the GET parameters are there, how can we use append() to include the other GET parameters in the paginated links without a whole chunk of if code messing up our blade template?



Solution 1:[1]

EDIT: Connor's comment with Mehdi's answer are required to make this work. Thanks to both for their clarifications.

->appends() can accept an array as a parameter, you could pass Input::except('page'), that should do the trick.

Example:

return view('manage/users', [
    'users' => $users->appends(Input::except('page'))
]);

Solution 2:[2]

I think you should use this code in Laravel version 5+. Also this will work not only with parameter page but also with any other parameter(s):

$users->appends(request()->input())->links();

Personally, I try to avoid using Facades as much as I can. Using global helper functions is less code and much elegant.

UPDATE:

Do not use Input Facade as it is deprecated in Laravel v6+

Solution 3:[3]

You could use

->appends(request()->query())

Example in the Controller:

$users = User::search()->order()->with('type:id,name')
    ->paginate(30)
    ->appends(request()->query());

return view('users.index', compact('users'));

Example in the View:

{{ $users->appends(request()->query())->links() }}

Solution 4:[4]

Be aware of the Input::all() , it will Include the previous ?page= values again and again in each page you open !
for example if you are in ?page=1 and you open the next page, it will open ?page=1&page=2
So the last value page takes will be the page you see ! not the page you want to see

Solution : use Input::except(array('page'))

Solution 5:[5]

Laravel 7.x and above has added new method to paginator:

->withQueryString()

So you can use it like:

{{ $users->withQueryString()->links() }}

For laravel below 7.x use:

{{ $users->appends(request()->query())->links() }}

Solution 6:[6]

Not append() but appends() So, right answer is:

{!! $records->appends(Input::except('page'))->links() !!}

Solution 7:[7]

LARAVEL 5

The view must contain something like:

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

Solution 8:[8]

Use this construction, to keep all input params but page

{!! $myItems->appends(Request::capture()->except('page'))->render() !!}

Why?

1) you strip down everything that added to request like that

  $request->request->add(['variable' => 123]);

2) you don't need $request as input parameter for the function

3) you are excluding "page"

PS) and it works for Laravel 5.1

Solution 9:[9]

Include This In Your View Page

 $users->appends(Input::except('page'))

Solution 10:[10]

for who one in laravel 5 or greater in blade:

{{ $table->appends(['id' => $something ])->links() }}

you can get the passed item with

$passed_item=$request->id;

test it with

dd($passed_item);

you must get $something value

Solution 11:[11]

In Laravel 7.x you can use it like this:

{{ $results->withQueryString()->links() }}

Solution 12:[12]

Pass the page number for pagination as well. Some thing like this

$currentPg = Input::get('page') ? Input::get('page') : '1';
    $boards = Cache::remember('boards'.$currentPg, 60, function(){ return WhatEverModel::paginate(15); });

Solution 13:[13]

In Your controller after pagination add withQueryString() like below

$post = Post::paginate(10)->withQueryString();

Solution 14:[14]

Many solution here mention using Input...

Input has been removed in Laravel 6, 7, 8

Use Request instead.

Here's the blade statement that worked in my Laravel 8 project:

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

Where $data is the PHP object containing the paginated data.

Thanks to Alexandre Danault who pointed this out in this comment.