Skip to content

Commit

Permalink
Merge pull request #8 from surazdott/refactor/paginate
Browse files Browse the repository at this point in the history
[1.x.x] meta and links added in paginate method
  • Loading branch information
surazdott authored Nov 27, 2024
2 parents f45e4f4 + 46471e1 commit e55b2c9
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 15 deletions.
21 changes: 15 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,16 +127,13 @@ public function index()
{
$users = UserResource::collection(User::active()->paginate(10));

return api()->success('Data fetched successfully.', $users);
return api()->paginate('Data fetched successfully.', $users);
}

// Result
{
"success": true,
"message": "Request processed successfully.",
"total": 20,
"total_pages": 2,
"per_page": 10,
"message": "Data fetched successfully.",
"data": [
{
"id": 1",
Expand All @@ -146,7 +143,19 @@ public function index()
"id": 2",
"name": "Rabin....",
}
]
],
"links": {
"first": "http://example.com/api/v1/users?page=1",
"last": "http://example.com/api/v1/users?page=1",
"prev": null,
"next": null
},
"meta": {
"total": 0,
"current_page": 1,
"total_pages": 1,
"per_page": 10
}
}
```

Expand Down
20 changes: 14 additions & 6 deletions src/Http/ApiResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,27 @@ public function paginate(string $message, mixed $data = []): JsonResponse
$response = [
'success' => true,
'message' => $message,
'data' => $data,
];

$resource = $data;

if ($resource instanceof JsonResource) {
$response['total'] = $data->total();
$response['total_pages'] = $data->lastPage();
$response['per_page'] = $data->perPage();
$response['data'] = $data;
$response['links'] = [
'first' => $data->url(1),
'last' => $data->url($data->lastPage()),
'prev' => $data->previousPageUrl(),
'next' => $data->nextPageUrl(),
];

$response['meta'] = [
'total' => $data->total(),
'current_page' => $data->currentPage(),
'total_pages' => $data->lastPage(),
'per_page' => $data->perPage(),
];
}

$response['data'] = $data;

return $this->toJson($response, 200);
}

Expand Down
15 changes: 12 additions & 3 deletions tests/ApiResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,19 @@ public function test_it_returns_a_paginated_response(): void
$this->assertEquals([
'success' => true,
'message' => $message,
'total' => $responseData['total'],
'total_pages' => $responseData['total_pages'],
'per_page' => $responseData['per_page'],
'data' => $responseData['data'],
'links' => [
'first' => $responseData['links']['first'],
'last' => $responseData['links']['last'],
'prev' => $responseData['links']['prev'],
'next' => $responseData['links']['next'],
],
'meta' => [
'total' => $responseData['meta']['total'],
'current_page' => $responseData['meta']['current_page'],
'total_pages' => $responseData['meta']['total_pages'],
'per_page' => $responseData['meta']['per_page'],
],
], $responseData);
}

Expand Down

0 comments on commit e55b2c9

Please sign in to comment.