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

Mushr00m's avatar

Paginate Eloquent collection

Hi,

I need to paginate an Eloquent collection (so after DB call). But from what I've read doing Paginator::make() doesn't exists anymore. I'm a little confused of how I should do it ? Examples would be nice.

My aim is to still do $items->appends(Request::only(['type', 'size']))->render() in my view and be able to keep the inputs in the pagination links and display them as I would have if the pagination was done on the query with ->paginate().

Thanks for your help

0 likes
17 replies
Mushr00m's avatar

@bluepenlabs : As I said in the topic, it's on a Collection, after the DB query. Here you talk about the classic paginate() on the query. If you prefer I need to paginate the result from :

$users = User::where('votes', '>', 100)->get();
bobbybouwmann's avatar
Level 88

You can create your own pagination object after it

$users = User::where('votes', '>', 100)->get();

$page = Input::get('page', 1); // Get the ?page=1 from the url
$perPage = 15; // Number of items per page
$offset = ($page * $perPage) - $perPage;

return new LengthAwarePaginator(
    array_slice($users->toArray(), $offset, $perPage, true), // Only grab the items we need
    count($users), // Total items
    $perPage, // Items per page
    $page, // Current page
    ['path' => $request->url(), 'query' => $request->query()] // We need this so we can keep all old query parameters from the url
);
21 likes
Mushr00m's avatar

@bobbybouwmann : Thanks for example. I'll try this. Feel kind of weird not to have it directly available in the Collection class.

jlrdw's avatar

@bobbybouwmann do you realize how many paginators you have written for people? Just think a little bit of study, understanding the MySQL limit clause, and a little math and they could write their own. But that is so 2015.

2 likes
Mushr00m's avatar

@jlrdw : Agreed but knowing how others do and not always code on our own way, open your mind to new methods, technics, patterns, etc... If not there is no point of using a Framework, working with Open Source and be part of this community. But it's just my opinion.

1 like
jlrdw's avatar

Yes I realize just don't hurt to search first.

sl0wik's avatar

bobbybouwmann: I suggest replacing $users->toArray() with $users->all().

"toArray also converts all of the collection's nested objects to an array. If you want to get the raw underlying array, use the all method instead."

bobbybouwmann's avatar

@leber My code is written almost a year ago! The collection class has since been changed a bit.

sl0wik's avatar

Sure. It's for people that googled this thread and receive error ;).

jeitnier's avatar

In my opinion this is a better way to paginate a Collection, using the Collections forPage method as an alternative to array_slice:

function collection_paginate($items, $per_page)
{
    $page   = Request::get('page', 1);
    $offset = ($page * $per_page) - $per_page;

    return new Illuminate\Pagination\LengthAwarePaginator(
        $items->forPage($page, $per_page)->values(),
        $items->count(),
        $per_page,
        Illuminate\Pagination\Paginator::resolveCurrentPage(),
        ['path' => Illuminate\Pagination\Paginator::resolveCurrentPath()]
    );
}

This simply keeps $items as a Collection because in Illuminate\Pagination\LengthAwarePaginator's constructor, $this->items is going to get transformed into a Collection anyway:

$this->items = $items instanceof Collection ? $items : Collection::make($items);

So basically this is just less overhead.

9 likes

Please or to participate in this conversation.