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?
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]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With