Download the PHP package hanksie/laravel-cursor-paginator-through-when-serialize without Composer
On this page you can find all versions of the php package hanksie/laravel-cursor-paginator-through-when-serialize. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download hanksie/laravel-cursor-paginator-through-when-serialize
More information about hanksie/laravel-cursor-paginator-through-when-serialize
Files in hanksie/laravel-cursor-paginator-through-when-serialize
Package laravel-cursor-paginator-through-when-serialize
Short Description add a method to register callback for transforming paginator's data when serializing
License MIT
Informations about the package laravel-cursor-paginator-through-when-serialize
Why make the package:
When using AbstractCursorPaginator::through()
or AbstractCursorPaginator::getCollection()->transform()
to transform each item, and serializing
(e.g. toArray(), toJson()) implicitly or obviously will give an error: Undefined index
,
Paginate and SimplePaginate don't have this problem.
This error occurs in AbstractCursorPaginator::getParametersForItem()
:
When pagination has previous page or next page, the method will be called. The error occurs because AbstractCursorPaginator need a field to get the page cursor. For example, we have a user model, it contain 'id' and 'name' field. We call User::cursorPaginate(n) to get a CursorPaginator instance, when the paginator serialized, AbstractCursorPaginator::getParametersForItem()
be called. Please see source link above, $item is a user model instance like
{ id: 1, name: 'someone', ...}
, $parameterName in callback block is "user.id"
, the result is 'id':
If transforming paginate items, because cannot access exist key, we will give an error: Undefined index: id
,
How the package fix it:
Overriding Illuminate\Pagination\AbstractCursorPaginator::through()
to register a callback for mapping each item in the slice of items to the "data" field when serialized. It's works because it not changed origin $items, it can avoid Undefined index error.
How to use:
Just use through()
method as before.