Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

uniqueginun's avatar

Laravel API Resources response doesn't contain links and meta for paginated collections

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?

0 likes
7 replies
Ashraam's avatar

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.

uniqueginun's avatar

yes you right. the meta and links disappear when I wrap the results in this resource

uniqueginun's avatar

but what is the issue?? the class generated by artisan command

martinbean's avatar

@uniqueginun Try just creating a “regular” API resource, and using the collection method on that:

php artisan make:resource TaskResource

You can then use this resource for both returning a single task, and multiple tasks.

You also just return the resource directly, instead of wrapping it in a response:

$tasks = $request->user()->tasks()->with('info.details')->paginate(5);

return TaskResource::collection($tasks);

I’m not sure where you’ve picked that up from as the docs don’t tell you to do that.

uniqueginun's avatar
uniqueginun
OP
Best Answer
Level 9

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.