The problem seems to be in the UserTaskResource.
If you return the tasks without passing by this class you should have the meta from the paginator.
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I created a larave api resource like this:
php artisan make:resource UserTasksResource --collection
in my controller:
$tasks = $request->user->tasks()->with(['info.details'])->paginate(5);
$tasks = new UserTasksResource($tasks );
return response([
'tasks' => $tasks
], 200);
now I the response is like:
{
"tasks": [
{...},
{...}, ...
]
}
first of all: where are the "links" and "meta" mentioned in the docs. second of all when I edited the resource controller like so:
/**
* Transform the resource collection into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'id' => $this->id,
'infoTitle' => $this->info->title
];
}
when I do this I get this error:
Undefined property: Illuminate\Pagination\LengthAwarePaginator::$id
I worked with API resources before and I didn't encounter such issues in the past, what I missing here?
I found the solution in stackoverflow. for transforming resource data you have to do the following:
public function toArray($request)
{
return $this->collection->map(function ($person) {
return [
'id' => $person->id,
'first_name' => $person->first_name,
'last_name' => $person->last_name,
'email' => $person->email,
'phone' => $person->phone,
'city' => $person->city,
'href' => [
'link' => route('person.show', $person->id),
],
];
});
}
it's clearly changed since last time I used API resources. because in the past you don't have to map over the collection because it's already single resource.
and when returning the collection you don't wrap it inside response helper. you just return it as it is.
Route::get('users', function (Request $request) {
$users = User::paginate(3);
$users = new \App\Http\Resources\User($users);
return $users;
});
Please or to participate in this conversation.