|
17 | 17 | <a href="#features">Features</a> | |
18 | 18 | <a href="#installation">Installation</a> | |
19 | 19 | <a href="#generating-dtos">Generating DTOs</a> | |
20 | | - <a href="#simple-dtos">Simple DTOs</a> | |
| 20 | + <a href="#more-dtos">More DTOs</a> | |
21 | 21 | <a href="#credits">Credits</a> | |
22 | 22 | <a href="#contributing">Contributing</a> |
23 | 23 | </p> |
|
37 | 37 | - Support casting of **nested data** |
38 | 38 | - Easily create **custom Type Casters** for your own needs |
39 | 39 | - Custom Data Mapping |
| 40 | +- Use DTOs for wrapping, typing and transforming API responses |
| 41 | +- **[Laravel Livewire](https://laravel-livewire.com/)** support |
40 | 42 |
|
41 | 43 | ## Installation |
42 | 44 |
|
@@ -955,8 +957,9 @@ class AttributesDTO extends ValidatedDTO |
955 | 957 | } |
956 | 958 | } |
957 | 959 | ``` |
| 960 | +## More DTOs |
958 | 961 |
|
959 | | -## Simple DTOs |
| 962 | +### Simple DTOs |
960 | 963 |
|
961 | 964 | If you don't need to validate the data, you can use the `SimpleDTO` class instead of the `ValidatedDTO` class. |
962 | 965 | The DTOs created with this class will not validate the data, but will still have all the other features of the `ValidatedDTO` class: |
@@ -1008,6 +1011,71 @@ To generate a `SimpleDTO` you can use the `--simple` flag: |
1008 | 1011 | php artisan make:dto SimpleUserDTO --simple |
1009 | 1012 | ``` |
1010 | 1013 |
|
| 1014 | +### Resource DTOs |
| 1015 | + |
| 1016 | +If you want to use DTOs to wrap, type and transform your API responses, you can use the `ResourceDTO` class. |
| 1017 | +This class will have the same features as the `SimpleDTO` class and will implement the `Illuminate\Contracts\Support\Responsable` interface: |
| 1018 | + |
| 1019 | +```php |
| 1020 | +class UserResourceDTO extends ResourceDTO |
| 1021 | +{ |
| 1022 | + public string $name; |
| 1023 | + |
| 1024 | + public string $email; |
| 1025 | + |
| 1026 | + public int $age; |
| 1027 | + |
| 1028 | + // Your DTO methods... |
| 1029 | +} |
| 1030 | +``` |
| 1031 | + |
| 1032 | +Then you can return your DTOs from your controllers: |
| 1033 | + |
| 1034 | +```php |
| 1035 | +class UserController extends Controller |
| 1036 | +{ |
| 1037 | + public function show(int $id) |
| 1038 | + { |
| 1039 | + return UserResourceDTO::fromModel(User::findOrFail($id)); |
| 1040 | + } |
| 1041 | +} |
| 1042 | +``` |
| 1043 | + |
| 1044 | +You can also return a collection/list of your DTOs as a response using the `ResourceDTO::collection()` method: |
| 1045 | + |
| 1046 | +```php |
| 1047 | +class UserController extends Controller |
| 1048 | +{ |
| 1049 | + public function index() |
| 1050 | + { |
| 1051 | + return UserResourceDTO::collection(User::all()); |
| 1052 | + } |
| 1053 | +} |
| 1054 | +``` |
| 1055 | + |
| 1056 | +This way every item in the collection will be converted to a `UserResourceDTO` instance before sending |
| 1057 | +the response to the client, using all the typing, casting and mapping features of your DTO class. |
| 1058 | + |
| 1059 | +To generate a `ResourceDTO` you can use the `--resource` flag: |
| 1060 | + |
| 1061 | +```bash |
| 1062 | +php artisan make:dto UserResourceDTO --resource |
| 1063 | +``` |
| 1064 | + |
| 1065 | +### Wireable DTOS |
| 1066 | + |
| 1067 | +If you're using **[Laravel Livewire](https://laravel-livewire.com/)**, you can turn your DTOs into **wireable** DTOs |
| 1068 | +by adding the `WendellAdriel\ValidatedDTO\Concerns\Wireable` trait to your DTOs: |
| 1069 | + |
| 1070 | +```php |
| 1071 | +class UserDTO extends ValidatedDTO |
| 1072 | +{ |
| 1073 | + use Wireable; |
| 1074 | + |
| 1075 | + // Your DTO code... |
| 1076 | +} |
| 1077 | +``` |
| 1078 | + |
1011 | 1079 | ## Credits |
1012 | 1080 |
|
1013 | 1081 | - [Wendell Adriel](https://github.com/WendellAdriel) |
|
0 commit comments