PHP code example of okriiza / laravel-api-response-formatter

1. Go to this page and download the library: Download okriiza/laravel-api-response-formatter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

okriiza / laravel-api-response-formatter example snippets




use Okriiza\ApiResponseFormatter\ApiResponse;

class UserController extends Controller
{
    public function show($id): JsonResponse
    {
        $user = User::find($id);

        if ($user) {
            return ApiResponse::success($user);
        } else {
            return ApiResponse::notFound(null, 'User not found');
        }
    }

    public function create(Request $request): JsonResponse
    {
        // Validation logic

        if ($validationFails) {
            return ApiResponse::failedValidation($validationErrors);
        }

        $user = User::create($request->all());

        return ApiResponse::created($user);
    }
}