satheez/api-response

Standard API response class


README

Standard API response class

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This is where your description should go. Limit it to a paragraph or two. Consider adding a small example.

Installation

You can install the package via composer:

composer require satheez/api-response

You can publish the config file with:

php artisan vendor:publish --tag="api-response-config"

Usage

Example

<?php

namespace App\Http\Controllers\Api;

use App\Models\User;
use Illuminate\Http\JsonResponse;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;

class UserController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return JsonResponse
     */
    public function index()
    {
        // todo, paginate the user data
        return api()->success(User::all()->toArray());
    }


    /**
     * Store a newly created resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @return JsonResponse
     */
    public function store(Request $request): JsonResponse
    {
        // todo validate the request data
        $user = User::create($request->all());
        return api()->created($user->toArray());
    }

    /**
     * Display the specified resource.
     *
     * @param int $id
     * @return JsonResponse
     */
    public function show($id): JsonResponse
    {
        $user = User::find($id);

        // todo update process
        return !empty($user)
            ? api()->success($user->toArray())
            : api()->notFound();
    }

    /**
     * Update the specified resource in storage.
     *
     * @param \Illuminate\Http\Request $request
     * @param int $id
     * @return JsonResponse
     */
    public function update(Request $request, $id): JsonResponse
    {
        // todo update process
        return api()->stored(User::find($id)->toArray());
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param int $id
     * @return JsonResponse
     */
    public function destroy($id): JsonResponse
    {
        User::destroy($id);
        return api()->deleted();
    }
}

Available methods

Success Methods ✌️

Error Status 💩

Extra methods 👨

Testing

composer test

Credits

License

The MIT License (MIT). Please see License File for more information.