Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel, how to convert a collection object into a builder object

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 ?

like image 427
Khaledman Avatar asked Feb 16 '18 17:02

Khaledman


People also ask

What is the use of collect () in Laravel?

Defining Laravel collections In the code snippet above, we used the collect() method to create a Collection instance from the defined array. We then modified each element to uppercase and remove empty elements. Laravel collections allow us to use several methods to analyze data.

What is serialize in Laravel?

October 5th, 2021. Eloquent Serialize is a Laravel package to serialize and unserialize Eloquent query builder objects. The EloquentSerialize service has two methods, serialize and unserialize . Given the following simple query, you can serialize the builder results: 1$data = \EloquentSerialize::serialize(

What is eloquent ORM in Laravel?

Eloquent is an object relational mapper (ORM) that is included by default within the Laravel framework. An ORM is software that facilitates handling database records by representing data as objects, working as a layer of abstraction on top of the database engine used to store an application's data.


2 Answers

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.

like image 151
Alexey Mezenin Avatar answered Oct 21 '22 17:10

Alexey Mezenin


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() !!}
like image 42
Erin Avatar answered Oct 21 '22 16:10

Erin