Giter Club home page Giter Club logo

bresources's Introduction

License Tests Latest Stable Version Total Downloads

Behamin Resources

Behamin standard formats for api responses.

Installation

composer require behamin/bresources

Output Format

Resources:

{
    "data": {
        "id": 1,
        "email": "[email protected]"
    },
    "message": "message",
    "error": {
        "message": "or error message",
        "errors": null
    }
}

ResourceCollection:

{
    "data": {
        "items": [],
        "count": 0,
        "sum": null
    },
    "message": null,
    "error": {
        "message": null,
        "errors": null
    }
}

On validation error for requests (with 422 status code):

{
    "data": null,
    "message": null,
    "error": {
        "message": "first error message in message bag",
        "errors": {
            "password": [
                "password field is required."
            ]
        }
    }
}

Usage

Create resources and requests with artisan commands and pass data, message, error_message or count to resources like following examples:

    public function index(EmailFilter $filters)
    {
        list($emails, $count) = Email::filter($filters);
        $emails = $emails->get();
        
        return EmailResource::collection(['data' => $emails, 'count' => $count]);
    }
    public function show(Email $email)
    {
        return new EmailResource(['data' => $email, 'message'=> 'email info.']);
    }

You can specify output fields from transformDataItem() method of resource classes.

<?php

namespace App\Http\Resources;

use Behamin\BResources\Resources\BasicResource;

class EmailResource extends BasicResource
{
    protected function transformDataItem($item)
    {
        return [
            'id' => $item->id,
            'email' => $item->email,
            'status' => $item->status
        ];
    }
}

Also, you can use apiResponse() helper function to directly send response

class PhoneController {

    public function show(Phone $phone)
    {
        return apiResponse()->data($phone)->message('phone info.')->status(200)->get();
    }
    
    public function index() {
        $phones = Phone::all();
        
        return apiResponse()->collection($phones, $phones->count())->message('phone info.')->status(200)->get();
    }
    
    public function update(Request $request, Phone $phone) {
        $isUpdated = $phone->update($request->all());
        
        if (!$isUpdated) {
            return apiResponse()->errors('phone is not updated');
        }
        
        return apiResponse()->data($phone)->message('phone is updated')->get();
    }
    
    public function delete(Phone $phone)
    {
        $phone->delete();
        
        return apiResponse()->message('phone info.')->next('https://debut.test')->status(200)->get();
    }
}

In above example message and status are optional, and their default value respectively are null and 200.

Resource

php artisan make:bresource ResourceClassName

Request

php artisan make:brequest RequestClassName

bresources's People

Contributors

hebrahimzadeh avatar mhabedini avatar omalizadeh avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

saber13812002

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.