PHP code example of danilowa / laravel-api-response-builder

1. Go to this page and download the library: Download danilowa/laravel-api-response-builder 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/ */

    

danilowa / laravel-api-response-builder example snippets


  /*
  |--------------------------------------------------------------------------
  | Custom Response Structure
  |--------------------------------------------------------------------------
  | Define a custom structure for responses. The example below 

  /*
  |--------------------------------------------------------------------------
  | Response Data Wrapper
  |--------------------------------------------------------------------------
  | If enabled, the response data will be wrapped in an additional 'data'
  | key. This is useful if you want a consistent structure for all responses.
  |
  */
  'wrap_data' => true,
  

  /*
  |--------------------------------------------------------------------------
  | Response Data Wrapper Key
  |--------------------------------------------------------------------------
  | This value sets the key used to wrap the response data. By default, it is
  | 'data', but you can customize it according to your API structure.
  |
  */
  'wrap_data_key' => 'items',
  

   $response = JsonResponse::success();
   

   $response = JsonResponse::success(['user' => $user]);
   

   $response = JsonResponse::success(['user' => $user], 'User fetched successfully.');
   

   $response = JsonResponse::success(['user' => $user], 'User fetched successfully.', true);
   

   $response = JsonResponse::success(['user' => $user], 'User fetched successfully.', true, 'customWrap');
   

   $response = JsonResponse::error(404);
   

   $response = JsonResponse::error(404, 'Resource not found.');
   

   $response = JsonResponse::error(400, 'Bad request', ['field' => 'username', 'error' => 'Username is 

   $response = JsonResponse::error(500, 'Internal server error', null, true);
   

   $response = JsonResponse::error(500, 'Internal server error', null, true, 'error');
   
bash
php artisan vendor:publish --provider="Danilowa\LaravelResponseBuilder\Providers\ResponseBuilderServiceProvider"
bash
php artisan vendor:publish --tag=lang