Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

frankone's avatar

query limit when using pagination

Hi there,

i have a question, came across this issue, looked up on internet and i see many others with the same difficulty and didnt see any solution, maybe you have some advice?

i have this app, which works with tables of products lets say, and i can create subsets (segments) of these products, based on some criteria. I can also limit the number of allowed items in a segment. So far so good. But when i use pagination to display the segment in a list in my view, the pagination overrides my limit and sets its own, and i get the full list of products in my table, although i had set a limit on that query. Any idea how can we use pagination on a limited set of results?

I know pagination works internally by setting its own limits, so i understand why this happens, but still, not ok and it should be a workaround.

Thanks a bunch!

0 likes
4 replies
Snapey's avatar

The paginator accepts the number of items per page, and will, of course, override any take() or limit that you may be applying.

Note that the paginator only works on the top level of items

frankone's avatar

okay, thanks for your reply.

But still, there should be some workaround for when you want to paginate a limited list, i'm sure this happens alot in many apps.

MichalOravec's avatar
Level 75

@frankone You can read this thread

https://laracasts.com/discuss/channels/eloquent/how-to-limit-pagination-to-x-itemspages

But you can make a macro for collection, where you make new paginate function

Put this to ServiceProvider

use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;

Collection::macro('paginate', function ($perPage, $total = null, $page = null, $pageName = 'page') {
    $page = $page ?: LengthAwarePaginator::resolveCurrentPage($pageName);

    return new LengthAwarePaginator($this->forPage($page, $perPage), $total ?: $this->count(), $perPage, $page, [
        'path' => LengthAwarePaginator::resolveCurrentPath(),
        'pageName' => $pageName,
    ]);
});

Then make your query get result with get() and you use your new paginate function on collection

$data = YourModel::limit(100)->get()->paginate(25);
frankone's avatar

Thank you MichalOravec! that looks to be spot on, i'll definitely try that! thanks again!

Please or to participate in this conversation.