Laravel API Response - Simple Laravel API response wrapper

Simple and ready to use API response wrapper for Laravel - Simple Laravel API response wrapper.

Installation

  1. Install package through composer: $ composer require obiefy/api-response
  2. publish config file : php artisan vendor:publish --tag=api-response

Basic usage

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);

}

Advanced usage

1. General example

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”}
]
}

2. Success response

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

Further reading

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

Laravel API Response - Simple Laravel API response wrapper
53.15 GEEK