8

I need to use paginate and simplepaginate on a collection, so i'm trying to convert the collection into a builder object. To do so I am thinking of creating a function that gets the id of every item in the collection and then builds a query with it, but that seemed to me like a lot of resources waisted, Is there a simpler way ?

Khaledman
  • 91
  • 1
  • 3
  • If you could provide some example that would be awesome... regenerating a collection to a builder is not a good idea... its better to paginate a collection directly when fetching a collection from the database.. https://laravel.com/docs/8.x/pagination#introduction – Jenuel Ganawed Oct 19 '21 at 14:30

2 Answers2

2

A better way to do this is to build paginator object manually using the existing collection.

From the docs:

Sometimes you may wish to create a pagination instance manually, passing it an array of items. You may do so by creating either an Illuminate\Pagination\Paginator or Illuminate\Pagination\LengthAwarePaginator instance, depending on your needs.

The Paginator class does not need to know the total number of items in the result set; however, because of this, the class does not have methods for retrieving the index of the last page. The LengthAwarePaginator accepts almost the same arguments as the Paginator; however, it does require a count of the total number of items in the result set.

In other words, the Paginator corresponds to the simplePaginate method on the query builder and Eloquent, while the LengthAwarePaginator corresponds to the paginate method.

Community
  • 1
  • 1
Alexey Mezenin
  • 158,981
  • 26
  • 290
  • 279
1

Building on what Alexey said, as alternative, you can build a Paginator from a collection manually. This is a simpler way without the waste of an additional query. e.g.

 // Collection $collection
 
$perPage       = 10;
$currentPage   = Illuminate\Pagination\Paginator::resolveCurrentPage() ?? 1;
$itemsOnPage   = $collection->skip(10 * ($currentPage-1))->take($perPage);
$paginatorPath = Illuminate\Pagination\Paginator::resolveCurrentPath();

$paginator     = new \Illuminate\Pagination\LengthAwarePaginator(
        $itemsOnPage,
        $collection->count(),
        $perPage,
        $currentPage,
        ['path' => $paginatorPath]
    );

Then in your view,

        {!! $paginator->render() !!}
Erin
  • 5,315
  • 2
  • 20
  • 36
  • The title of this question is "how to convert a collection object into a builder object" and the bounty request is "Looking for an answer that explicitly answers the question, rather than alternative approaches." -- The answer should not be about building new paginator instances. – Jesse Oct 17 '21 at 19:49
  • Actually, if you edit your answer so I can vote again, I will remove my downvote since this is actually useful/original code. – Jesse Oct 17 '21 at 19:55
  • Edited! (You can’t convert to a Builder instance because the query parameters don’t exist on the Collection instance, and it would be counter productive just for pagination. – Erin Oct 18 '21 at 06:34
  • And the OP does ask “Is there a simpler way” without the waste of resources – Erin Oct 18 '21 at 06:35
  • 1
    Thanks for the bounty @Jesse – Erin Oct 21 '21 at 19:37