Resources / Collections (Transforming)

Resources: https://laravel.com/docs/6.x/eloquent-resources

Generate with php artisan make:api:resource UserResource and php artisan make:api:resource UserResourceCollection

Change the Resource to extend from:

use Phpsa\LaravelApiController\Http\Resources\ApiResource for your resource use Phpsa\LaravelApiController\Http\Resources\ApiCollection for your resource collection

in your controller override the following params:

    protected $resourceSingle = UserResource::class;
    protected $resourceCollection = UserCollection::class;

The following properties are available on the resource object:

  • protected static $mapResources - maps related resources
  • protected static $defaultFields = null; - which fields are returned by default
  • protected static $allowedFields = null; - which fields are allowed to be returned
  • protected static $allowedScopes = null; - which scopes should be callable.

Snake vs Camel

Use this middleware to convert all camel to snake: \Phpsa\LaravelApiController\Http\Middleware\SnakeCaseInputs

set your request header X-Accept-Case-Type to either snake or camel to transform your data response keys to either snake or camel case.

Response modifications / post actions

In the controller the following methods can be overwritten:

  • handleIndexResponse
  • handleStoreResponse
  • handleShowResponse
  • handleUpdateResponse
  • handleDestroyResponse