Laravel Pagination links not including other GET parameters
Asked Answered
V

14

162

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

Problem: When there are some GET parameters in the URL, eg: http://site.example/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?

Veto answered 18/6, 2013 at 1:24 Comment(1)
you can simply do: {{ $users->withQueryString()->links() }} in laravel version 7 and aboveUnharness
D
144

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'))
]);
Diverticulitis answered 18/6, 2013 at 1:37 Comment(12)
Just to note for other Googlers coming this way - it's now plural appends, not append. The correct chaining would be something like $query->appends($foo)->links();Sandor
I found $query->appends(Input::except('page'))->links(); worked better. Otherwise, the page variable did not update when you clicked a page. Laravel 4.1.24.Muckworm
...and then I scrolled down to see Mehdi's solution.Muckworm
Or you can use Input::get() to get all the $_GET varsHoem
@Hoem Input::get returns all inputt'ed variables, both POST and GET.Lactation
LARAVEL 5: {!! $myItems->appends(Input::except('page'))->render() !!}Invariable
Just for anyone else searching, depending on your implementation it might also look something like this {!! with(new \App\Presenters\CustomPagerPresenter($results->appends(Input::except('page'))))->render() !!}Delivery
In laravel 5.2 you should use Request instead of InputRugging
tested now on laravel 6.2, the use of request global helper more elegant for me. {{$items->appends(request()->except('page'))->links()}}Biliary
Code is written as to use in controller, but Input is a view class... Hassan solution works on Laravel 7, down here:Shortsighted
Where is the Input? use \Symfony\Component\Console\Input; not work.Geneviegenevieve
@Geneviegenevieve Input no longer exists In larvel 6,7,8 Version. Use Request instead of Input. Based on the Laravel docs, since version 6.x Input has been removed. The Input Facade Likelihood Of Impact: Medium The Input facade, which was primarily a duplicate of the Request facade, has been removed. If you are using >the Input::get method, you should now call the Request::input method. All other calls to the Input facade may >simply be updated to use the Request facade. Source: laracasts.com/discuss/channels/laravel/…Diverticulitis
S
160

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+

Sheugh answered 24/7, 2016 at 20:32 Comment(6)
This worked for laravel 5.6, using the accepted answer solution caused an error for me.Reproductive
I also implemented this successfully on a 5.3 project; I prefer this simple method as wellSterling
Also, I've noticed that you don't need to use except('page') with this solution.Pantograph
But what global helper functions but a facades you are trying to avoid?..Sibilate
This worked in laravel 8, more useful and simple solution.Contagium
@Sibilate Request and Input fecadesSheugh
D
144

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'))
]);
Diverticulitis answered 18/6, 2013 at 1:37 Comment(12)
Just to note for other Googlers coming this way - it's now plural appends, not append. The correct chaining would be something like $query->appends($foo)->links();Sandor
I found $query->appends(Input::except('page'))->links(); worked better. Otherwise, the page variable did not update when you clicked a page. Laravel 4.1.24.Muckworm
...and then I scrolled down to see Mehdi's solution.Muckworm
Or you can use Input::get() to get all the $_GET varsHoem
@Hoem Input::get returns all inputt'ed variables, both POST and GET.Lactation
LARAVEL 5: {!! $myItems->appends(Input::except('page'))->render() !!}Invariable
Just for anyone else searching, depending on your implementation it might also look something like this {!! with(new \App\Presenters\CustomPagerPresenter($results->appends(Input::except('page'))))->render() !!}Delivery
In laravel 5.2 you should use Request instead of InputRugging
tested now on laravel 6.2, the use of request global helper more elegant for me. {{$items->appends(request()->except('page'))->links()}}Biliary
Code is written as to use in controller, but Input is a view class... Hassan solution works on Laravel 7, down here:Shortsighted
Where is the Input? use \Symfony\Component\Console\Input; not work.Geneviegenevieve
@Geneviegenevieve Input no longer exists In larvel 6,7,8 Version. Use Request instead of Input. Based on the Laravel docs, since version 6.x Input has been removed. The Input Facade Likelihood Of Impact: Medium The Input facade, which was primarily a duplicate of the Request facade, has been removed. If you are using >the Input::get method, you should now call the Request::input method. All other calls to the Input facade may >simply be updated to use the Request facade. Source: laracasts.com/discuss/channels/laravel/…Diverticulitis
S
79

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() }}
Santee answered 24/11, 2017 at 13:32 Comment(1)
This is the suggested way by Mohamed Said (Laravel employee), see: github.com/laravel/framework/issues/…Upbraid
C
50

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() }}
Complacent answered 30/7, 2020 at 10:6 Comment(7)
Wow its amazing just a single line. I missed the docs. Although 7.x is loveContingent
you provide 2 approaches .Please differentiate them and provide info which one is better and more usefulContingent
@Contingent behind the hood both are same. Laravel just adopted it on its latest version.Complacent
@RikeshShrestha Is there a way to urlencode the query parameters with that?Oilstone
if you have a blank parameter value also use if (isset($_GET['search_text'])) { $search_text = $_GET['search_text']; } else { $search_text = ""; }Neighbor
This should be the accepted answer. It provides answer for different versions of laravel.Swordbill
I'm so confused, I see this in the documentation, and I'm on Laravel 9, but for some reason no matter what parameter I pass as a get parameter it won't be added to the paginated parameter, lets's say I input a parameter ?topic='whatever', then this topic parameter won't be added to the urlSeamark
A
41

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'))

Akilahakili answered 19/11, 2013 at 16:39 Comment(0)
C
29

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

{!! $records->appends(Input::except('page'))->links() !!}
Crayton answered 25/11, 2014 at 7:16 Comment(0)
A
12

In Your controller after pagination add withQueryString() like below

$post = Post::paginate(10)->withQueryString();
Asymptomatic answered 27/4, 2022 at 5:28 Comment(0)
I
11

LARAVEL 5

The view must contain something like:

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

Invariable answered 11/10, 2015 at 23:0 Comment(0)
M
5

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

Marty answered 15/9, 2016 at 3:1 Comment(0)
D
4

Include This In Your View Page

 $users->appends(Input::except('page'))
Dyer answered 11/12, 2017 at 5:48 Comment(0)
O
4

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

Officialese answered 13/8, 2018 at 19:24 Comment(0)
M
3

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

{{ $results->withQueryString()->links() }}
Mastectomy answered 30/11, 2020 at 7:39 Comment(0)
W
-1

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); 
});
Woodprint answered 5/7, 2017 at 9:50 Comment(0)
T
-1

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.

Tjon answered 20/11, 2021 at 19:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.