Reputation: 1353
I'm returning two Laravel Api Collections and to do so I'm using
return response()->json([
'user' => new UserResource($contractor_user),
'maintenance' => new MaintenanceCollection($contractor_maintenances),
]);
On the variable $contractor_maintenances
I have a paginate method
$contractor_maintenances = Maintenance::filter($request)
->whereHas('contactedContractor', function ($query) use ($contractor_user_id) {
return $query->where('user_id', '=', $contractor_user_id);
}) ->with([
'contactedContractor'
])
->latest('maintenances.created_at')
->paginate(2);
My problem is that if I return the maintenance collection on its own
return new MaintenanceCollection($contractor_maintenances);
then the pagination works but when I call in two resources using
return response()->json([
'user' => new UserResource($contractor_user),
'maintenance' => new MaintenanceCollection($contractor_maintenances),
]);
the pagination meta data is not presented. How do I solve this problem? Thanks.
Upvotes: 0
Views: 464
Reputation: 41
I figured out that you can manually insert the pagination meta data in a JsonResponse like this:
return response()->json([
'data' => new DataCollection($data),
'meta' => [
'total' => $data->total(),
'currentPage' => $data->currentPage(),
'lastPage' => $data->lastPage(),
'perPage' => $data->perPage(),
],
'links' => [
'first' => $data->url(1),
'last' => $data->url($data->lastPage()),
'prev' => $data->previousPageUrl(),
'next' => $data->nextPageUrl(),
],
]);
Upvotes: 2