Somewhere between 5.8 and 6.0, PaginatedResourceResponse::toResponse() changed.
In 5.5, in an API controller, I did a query and passed it to ->paginate($num) instead of to get() and then pushed the result set into a new ApplicationCollectionResource();. The result of that (a resource) was then returned.
This would work.
In 6.0, when the resource is returned. it winds up in PaginatedResourceResponse::toResponse where the return is $item->resource, while $item is an array, not a collection. This is what changed between 5.8 and 6.0.
I've seen no examples of how to do this in 6.0 or better. My resource has a toArray() function, and I've seen hints in postings about a collection() method, but I have no example of how to write one. Note that the superclass, ResourceCollection, does not define a collection method as an alternative to the toArray method.
Can somebody point me to a >=6.0 example of doing a query and returning the result set in a using Illuminate\Http\Resources\Json\PaginatedResourceResponse?
I have in my controller:
public function newrecords(Request $request)
{
// get some records
$datarecords = MyData::where('status','new')->pagenate(50);
// this returns an \Illuminate\Pagenation\LengthAwarePaginator object
$resource = new MyResource($datarecords)
// Now we return the resource:
return $resource;
}
and my resource contains
public function toArray($request)
{
$result = $this->collection->transform(function ($o) {
return[
'key' => $o->value,
...
];
})->toArray();
return $result;
}