Simple and ready to use API response wrapper for Laravel - Simple Laravel API response wrapper.
$ composer require obiefy/api-response
php artisan vendor:publish --tag=api-response
Create and return JSON response:
use Obiefy\API\Facades\API; ... public function index() { $users = User::all();return API::response(200,'users list', $users);
}
Or you can use helper function:
use Obiefy\API\Facades\API;
…
public function index()
{
$users = User::all();return api()->response(200, 'users list', $users);
}
use Obiefy\API\Facades\API;
…
public function index()
{
$users = User::all();return API::response(200, 'users list', $users);
}
result:
{
“STATUS”: 200,
“MESSAGE”: “users list”,
“DATA”: [
{“name”: “user name”}
]
}
return api()->ok('Success message`, [
‘name’ => ‘Obay Hamed’
]);
result:
{
“STATUS”: 200,
“MESSAGE”: “Success message”,
“DATA”: {“name”: “Obay Hamed”}
}
you can also return success message with out passing parametters
return api()->ok();
in this case response message will be the default message from config file config(‘api.messages.success’)
the same thing for api()->notFound()
and api()->validation()
.
Source Code: https://github.com/obiefy/api-response
Thanks for reading ❤
If you liked this post, share it with all of your programming buddies!
Follow me on Facebook | Twitter
☞ Laravel 5.8 Tutorial for Beginners
☞ Tutorial Laravel 6 with Docker and Docker-Compose
☞ How to Laravel 6 Database Migrations - Add Admin Role to Users
☞ Laravel 6 Release New Features and Upgrade
Laravel 6 CRUD Application Tutorial
#laravel #api