Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel pagination per_page/limit does not get added to nextPageUrl

Tags:

laravel

When using Model::paginate($per_page) the pagination is returned including the per_page variable. The problem is that the when you get the nextPageUrl() it only provides a ?page=x url variable, so if you want to specify the per_page in the url e.g.

https://example.com/api/users?page=1&per_page=100

It doesn't get added by the LengthAwarePaginator to the next/prev urls.

Is there something I'm missing or do I have to manually append this to the URL?

like image 414
Wasim Avatar asked Dec 14 '22 13:12

Wasim


1 Answers

If you want to add that to every single url there's a method in your paginator that allows you to add query strings.

Laravel 5.4+ :

//fetch the get parameter per_page, if doesn't exists defaults it to 100
$per_page = \Request::get('per_page') ?: 100;
$users = App\User::paginate($per_page);
$users->appends(['per_page' => $per_page])
return view('users', ['users' => $users]);

Old solution 5.3:

//fetch the get parameter per_page, if doesn't exists defaults it to 100
$per_page = \Request::get('per_page') ?: 100;
$users = User::paginate($per_page);
$users->addQuery('per_page', $per_page);
return view('users', ['users' => $users]);
like image 114
Fabio Antunes Avatar answered Apr 27 '23 01:04

Fabio Antunes