I have an API and I'm using Resource and Resource Collections.
I have an API controller with the following
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$stores = Store::paginate(2);
return $this->sendResponse(new StoreCollection($stores), 'Stores retrieved successfully.');
}
My API Base controller is as follows:
/**
* @param $result
* @param string $message
*
* @return \Illuminate\Http\JsonResponse
*/
public function sendResponse($result, string $message): \Illuminate\Http\JsonResponse
{
$response = [
'success' => true,
'status' => 200,
'code' => 'success',
'message' => $message,
'data' => $result,
];
return response()->json($response, 200);
}
The response is this:
{
"success": true,
"status": 200,
"code": "success",
"message": "Stores retrieved successfully.",
"data": [
{
"id": 1,
"name": "Toronto-Central Dufferin & Bloor",
"location_number": 648,
"latitude": "43.6574",
"longitude": "-79.43663",
"tel": "531-6074",
"fax": "531-7237",
"address": "900 DUFFERIN STREET",
"address2": "UNIT 366",
"postal_code": "M6H 4A9",
"city": "TORONTO-CENTRAL",
"district": "Toronto West",
"manager_name": "SHAWN STEDMANN",
"created_at": "12/08/2020",
"updated_at": "12/08/2020"
},
{
"id": 2,
"name": "Toronto-Central Dufferin & Bloor",
"location_number": 648,
"latitude": "43.6574",
"longitude": "-79.43663",
"tel": "531-6074",
"fax": "531-7237",
"address": "900 DUFFERIN STREET",
"address2": "UNIT 366",
"postal_code": "M6H 4A9",
"city": "TORONTO-CENTRAL",
"district": "Toronto West",
"manager_name": "SHAWN STEDMANN",
"created_at": "12/08/2020",
"updated_at": "12/08/2020"
}
]
}
How am I able to add the meta array to the response with the paginate values?? Or is there a better way to do this?
I want to have the response so it's like this:
{
"success": true,
"status": 200,
"code": "success",
"message": "Stores retrieved successfully.",
"data": [
{
"id": 1,
"name": "Toronto-Central Dufferin & Bloor",
"location_number": 648,
"latitude": "43.6574",
"longitude": "-79.43663",
"tel": "531-6074",
"fax": "531-7237",
"address": "900 DUFFERIN STREET",
"address2": "UNIT 366",
"postal_code": "M6H 4A9",
"city": "TORONTO-CENTRAL",
"district": "Toronto West",
"manager_name": "SHAWN STEDMANN",
"created_at": "12/08/2020",
"updated_at": "12/08/2020"
},
{
"id": 2,
"name": "Toronto-Central Dufferin & Bloor",
"location_number": 648,
"latitude": "43.6574",
"longitude": "-79.43663",
"tel": "531-6074",
"fax": "531-7237",
"address": "900 DUFFERIN STREET",
"address2": "UNIT 366",
"postal_code": "M6H 4A9",
"city": "TORONTO-CENTRAL",
"district": "Toronto West",
"manager_name": "SHAWN STEDMANN",
"created_at": "12/08/2020",
"updated_at": "12/08/2020"
}
],
"links": {
"first": "http://localhost/api/v1/stores?page=1",
"last": "http://localhost/api/v1/stores?page=347",
"prev": null,
"next": "http://localhost/api/v1/stores?page=2"
},
"meta": {
"current_page": 1,
"from": 1,
"last_page": 347,
"path": "http://localhost/api/v1/stores",
"per_page": 2,
"to": 2,
"total": 693
}
}