ideas
ideas copied to clipboard
Paginator->items() as a Collection?
Although the $items
property within Illuminate\Pagination\AbstractPaginator
is an instance of \Illuminate\Support\Collection
, when calling the items()
method on a paginator object, an array is returned (by calling all()
on the collection).
Is there any appetite for a small PR to change this to return an instance of a Collection
instead?
I've come across cases where I need the items from a paginated query as a collection, and it seems cumbersome to have to manually create a new collection instance in each case, when the paginator could do it. Also, as collections are used in most other places in the framework, it feels better than just an array.
I assume it returns an array at present to stop the underlying items collection being modified outside of the paginator? If so, could the items()
method return a new Collection instance - something like;
public function items()
{
return new Collection($this->items->all());
}
I thought about suggesting using $this->items->values()
, but that would reset the array keys.
As collections implement the ArrayAccess
interface anyway, there should be limited issues with backwards comparability? The only issue I can think of is if people are using the return value of items()
in a method / function that is type hinted as an array?
Alternatively, to keep total backwards compatibility, a new method could be added to AbstractPaginator
that returns an items collection - maybe named something like collection()
or toCollection()
?
I think this might be a duplicate idea, see: https://github.com/laravel/ideas/issues/1564