158

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?

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Nyxynyx
  • 61,411
  • 155
  • 482
  • 830
  • 3
    you can simply do: {{ $users->withQueryString()->links() }} in laravel version 7 and above – Ali_Hr Nov 28 '21 at 06:47

14 Answers14

159

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+

Amir Hassan Azimi
  • 9,180
  • 5
  • 32
  • 43
143

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'))
]);
Limon Monte
  • 52,539
  • 45
  • 182
  • 213
Alexandre Danault
  • 8,602
  • 3
  • 30
  • 33
  • 39
    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();` – Connor Peet Oct 06 '13 at 13:01
  • 6
    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. – bitlather Mar 22 '14 at 23:19
  • 1
    ...and then I scrolled down to see Mehdi's solution. – bitlather Mar 22 '14 at 23:20
  • Or you can use Input::get() to get all the $_GET vars – Benubird Apr 07 '14 at 10:01
  • @Benubird Input::get returns all inputt'ed variables, both POST and GET. – chesscov77 Jun 19 '14 at 19:46
  • 2
    LARAVEL 5: `{!! $myItems->appends(Input::except('page'))->render() !!}` – ecairol Oct 11 '15 at 22:59
  • 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() !!}` – joevallender Dec 17 '15 at 04:39
  • 2
    In laravel 5.2 you should use Request instead of Input – Christophvh Sep 16 '16 at 12:20
  • tested now on laravel 6.2, the use of request global helper more elegant for me. `{{$items->appends(request()->except('page'))->links()}}` – Tomer Ofer Feb 05 '20 at 13:39
  • Code is written as to use in controller, but Input is a view class... Hassan solution works on Laravel 7, down here: – Gediminas Šukys Apr 16 '21 at 07:14
  • Where is the Input? use \Symfony\Component\Console\Input; not work. – user2301515 Sep 20 '21 at 05:50
  • @user2301515 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: https://laracasts.com/discuss/channels/laravel/class-input-not-found-1?page=1&replyId=656314 – Alexandre Danault Sep 21 '21 at 11:19
76

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() }}
AKOP
  • 1,000
  • 7
  • 10
  • 6
    This is the suggested way by Mohamed Said (Laravel employee), see: https://github.com/laravel/framework/issues/19441#issuecomment-305526428 – Roy May 30 '18 at 10:12
45

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() }}
Shrestharikesh
  • 820
  • 7
  • 13
  • 2
    Wow its amazing just a single line. I missed the docs. Although 7.x is love – MR_AMDEV Aug 26 '20 at 20:06
  • you provide 2 approaches .Please differentiate them and provide info which one is better and more useful – MR_AMDEV Aug 26 '20 at 20:07
  • 1
    @MR_AMDEV behind the hood both are same. Laravel just adopted it on its latest version. – Shrestharikesh Aug 29 '20 at 01:14
  • @RikeshShrestha Is there a way to urlencode the query parameters with that? – pimarc Mar 28 '21 at 14:00
  • if you have a blank parameter value also use if (isset($_GET['search_text'])) { $search_text = $_GET['search_text']; } else { $search_text = ""; } – DragonFire Dec 14 '21 at 10:38
  • 1
    This should be the accepted answer. It provides answer for different versions of laravel. – Aman Jul 23 '22 at 08:35
  • 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 url – ii iml0sto1 Oct 24 '22 at 12:22
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'))

Mehdi Maghrouni
  • 1,529
  • 22
  • 23
29

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

{!! $records->appends(Input::except('page'))->links() !!}
Bald
  • 2,156
  • 3
  • 24
  • 33
11

LARAVEL 5

The view must contain something like:

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

ecairol
  • 6,233
  • 1
  • 27
  • 26
10

In Your controller after pagination add withQueryString() like below

$post = Post::paginate(10)->withQueryString();
Miraj Khandaker
  • 772
  • 10
  • 11
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

Yevgeniy Afanasyev
  • 37,872
  • 26
  • 173
  • 191
4

Include This In Your View Page

 $users->appends(Input::except('page'))
Siva Ganesh
  • 1,415
  • 2
  • 16
  • 28
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

Mostafa Asadi
  • 339
  • 4
  • 8
3

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

{{ $results->withQueryString()->links() }}
Mostafa Norzade
  • 1,578
  • 5
  • 24
  • 40
-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); 
});
roapp
  • 530
  • 6
  • 17
Cengkuru Michael
  • 4,590
  • 1
  • 33
  • 33
-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.

Max Stevens
  • 101
  • 1
  • 7