Giter VIP home page Giter VIP logo

jsonable's Introduction

Jsonable

Laravel JSON response trait. This trait makes it easy for any controller to return a JSON response with the appropriate HTTP status code.

Installation

Via composer:

composer require imdhemy/jsonable

Usage

All that you need is to use the Jsonable trait inside your controller.

Example:

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Imdhemy\Jsonable\Jsonable;

class CountryController extends Controller
{
    use Jsonable;

    public function index()
    {
        $data = \App\Country::get();
        return $this->ok($data, 'countries');
    }
}

The previous code will return a JSON response like the following:

  {
    "countries": [
        {
            "name": "Egypt",
            "capital": "Cairo",
        },
           ...
    ]
}

The parent key is optional, you can ommit it:

$data = \App\Country::get();
return $this->ok($data);

The response will be like the following:

[
 {
  "name": "Egypt",
  "capital": "Cairo",
  },
        ...
]

Available methods

Success Methods

Method Status code Description
ok 200 Successful get, patch (return a JSON object)
created 201 Successful post (return a JSON object)
noContent 204 Successful delete

Error Status

Method Status code Description
unauthorized 401 Not authenticated
invalid 403 Authenticated, but no permissions
notFound 404 Not Found
invalid 422 Validation

Extra methods

Method Status code Description
accepted 202 Successful post, delete, path - async
badRequest 400 The request could not be understood by the server due to malformed syntax
paymentRequired 402 Payment required

jsonable's People

Contributors

abdelrahman-waziry avatar imdhemy avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

canedoc summercms

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.