Laravel API Response - Simple Laravel API response wrapper

Laravel API Response - Simple Laravel API response wrapper

Laravel API Response is a good package that we can use to create API responses easily.

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

What's new in Bootstrap 5 and when Bootstrap 5 release date?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Random Password Generator Online

HTML Color Picker online | HEX Color Picker | RGB Color Picker

Laravel Passport API Authentication & Personal Access Tokens

In this tutorial, I’ll show you how to authenticate users using Laravel Passport in Laravel API. You will also learn how to use the Vuex store to manage the ...

Create Asp Net Web API for CRUD operation | CRUD Using Asp Net Api

LIKE | COMMENT | SHARE | SUBSCRIBE Web API CRUD Operations Using Asp Net MVC and Entity Framework Contents * Web API Project For CRUD Operations - Insert Upd...

Create web api project

LIKE | COMMENT | SHARE | SUBSCRIBE Web API with MVC Project Visual Studio (2013/2015/2017/2019) includes Web API template which creates a new Web API project...

Create Api controller and action

LIKE | COMMENT | SHARE | SUBSCRIBE Web API Controller We created Web API with MVC project in the previous section where it generated a simple controller. Her...

API call using postman

LIKE | COMMENT | SHARE | SUBSCRIBE Postman is a great tool when trying to dissect RESTful APIs made by others or test ones you have made yourself. It offers ...