satheez / api-response
Standard API response class
v1.1
2022-07-24 05:46 UTC
Requires
- php: ^8.1
- illuminate/contracts: ^9.0
- spatie/laravel-package-tools: ^1.9.2
Requires (Dev)
- laravel/pint: ^1.0
- nunomaduro/collision: ^6.0
- nunomaduro/larastan: ^2.0.1
- orchestra/testbench: ^7.0
- pestphp/pest: ^1.21
- pestphp/pest-plugin-laravel: ^1.1
- phpstan/extension-installer: ^1.1
- phpstan/phpstan-deprecation-rules: ^1.0
- phpstan/phpstan-phpunit: ^1.0
- phpunit/phpunit: ^9.5
- spatie/laravel-ray: ^1.26
This package is auto-updated.
Last update: 2024-11-09 17:24:58 UTC
README
Standard API response class
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.