giagara's avatar

Error using nested ApiResource

Hello everybody, while developing a personal package I've found a strange issue. My goal is to return an array from a controller, thats contains to element: ['columns'] => is an array of array, nothing special ['data'] => is the result of a paginated query builder (spatie laravel-query-builder/v3).

What i can see is that data contains an ApiResource, i lost the pagination properties, and get only the users section. I tried also this:

If i execute

$users = QueryBuilder::for(User::class)->with('roles')->paginate();
return UserResource::collection($users);

Everything went good, andi get data with users and pagination properties!

But if i want to introduce ApiResource like this:

$users = QueryBuilder::for(User::class)->with('roles')->paginate();
return [
    'columns' => [],
    'users' => UserResource::collection($users)
];

I get columns, and users. Inside users i get directly the users Resource, without pagination properties.

Am i missing something?

thanks!

0 likes
2 replies
giagara's avatar

another note: I've also tried:

$users = QueryBuilder::for(User::class)->with('roles')->paginate();
$test = UserResource::collection($users);
$test = [
    'columns' => [],
    'users' => $test
];
//dd($test); //<- here i can see the pagination detail
return $test; //<- here i CAN'T see the pagination detail, just users

Please or to participate in this conversation.