Unification of API responses for Laravel projects inspired by telegram api responses
- PHP >= 8.1
- Laravel >= 10
For lesser versions of Laravel or PHP, use the v2
composer require kolirt/laravel-api-response
return api()
->error()
->setCode(400) // default code 400
->setDescription(['Description #1', 'Description #2'])
// or
->setDescription('Description')
->setData(['Data #1', 'Data #2'])
// or
->setData('Data')
->render();
[
'ok' => false,
'error_code' => 400,
'description' => ['Description #1', 'Description #2'],
// or
'description' => 'Description',
'result' => ['Data #1', 'Data #2'],
// or
'result' => 'Data',
]
return api()
->success()
->setCode(200) // default code 200
->setDescription(['Description #1', 'Description #2'])
// or
->setDescription('Description #1')
->setData(['Data #1', 'Data #2'])
// or
->setData('Data')
->render();
[
'ok' => true,
'description' => ['Description #1', 'Description #2'],
// or
'description' => 'Description',
'result' => ['Data #1', 'Data #2'],
// or
'result' => 'Data',
]
Mark response as error
return api()->error();
Mark response as success
return api()->success();
Set specific code to response. Available codes
return api()->setCode($code);
Add description to response
return api()->setDescription(['Description #1', 'Description #2']);
// or
return api()->setDescription('Description');
Add errors to response
return api()->setErrors([
'first_name' => 'Error message',
'last_name' => ['Error message 1', 'Error message 2']
]);
Abort response with error
return api()->abort('Error message', 400);
Add cookie to response
return api()->cookie(cookie('token', 'asdsadsadas', 60 * 3));
Add data to response
return api()->setData(['Data #1', 'Data #2']);
// or
return api()->setData('Data');
Render response
return api()->render();
Check closed issues to get answers for most asked questions
Check out my other packages on my GitHub profile