ideas
ideas copied to clipboard
[Proposal] Geared Pagination
Allow for 'geared' changes in the number of records returned in a pagination.
This would allow setting the first page to display 15 records, the second 30 and so on. Once it hits 100, it continues to use 100 as its return count. For example
$gears = [15,30,50,100];
Saw an article about this feature being available in Rails/Basecamp. https://medium.com/@liroy/geared-pagination-in-rails-behind-the-scenes-61d9e227540e
https://github.com/basecamp/geared_pagination
Implementations would be as simple as a new method alongside
->paginate()
such as
->gearedPaginate()
You could pass the array of values you want into this
$users = DB::table('users')->gearedPaginate([15,30,50,100]);
or use the existing method and type switch
$users = DB::table('users')->paginate([15,30,50,100]);
This may be the best way to implement as the user has to do the least different to use it.
Only difficulties I can see are the current pagination setup is designed for a per_page, and it might be a bit un-DRY alongside the current implementation...
Thoughts? 😄
I prefer overloading paginate to take an array rather than adding an extra method. When it comes to DRY, there should not be any issue. All code can be shared, except for process converting [$page, $perPage] pair to [$offset, $limit].
edit: And method converting $totalResults to $totalPages
Hi @hubertnnn , Totally agree - overloading to accept an integer or an array of integers is the best / simplest solution. Had a dig about this weekend and it seems entirely possibly.
I would consider using the per_page
as the current page count limit, but having a second prop ratios
that is used to calculate the per_page
value.
Possible, plausible - now for me to have time to make the change! :)
Just found this issue. We already have Cursor Pagination and I think implementing the per page ratio would be really cool. Something like this:
Route::get('users', function () {
return User::query()
->latest()
->latest('id')
->cursorPaginate([15, 30, 50, 100]);
});
And it would return 15 items on the first page, 30 on the second page, 50 items on the third, and 100 items on the fourth page and forward. I thought about adding another param for the ratio, but honestly, I think it's OK to have the per_page
be either an int or an array meaning the ratio. But definitely, something that would be for Laravel 9 or something (I guess it's a BC, even though the old behavior would still work).
One problem I see is I couldn't find a way to tell which page we're on in the cursor when using the Cursor Pagination.