Resource DTOs
If you want to use DTOs to wrap, type and transform your API responses, you can use the ResourceDTO
class. This class will have the same features as the SimpleDTO
class and will implement the Illuminate\Contracts\Support\Responsable
interface:
Then you can return your DTOs from your controllers:
You can also return a collection/list of your DTOs as a response using the ResourceDTO::collection()
method:
This way every item in the collection will be converted to a UserResourceDTO
instance before sending the response to the client, using all the typing, casting and mapping features of your DTO class.
To generate a ResourceDTO
you can use the --resource
flag:
Last updated