|
| 1 | +# Usage Guide |
| 2 | + |
| 3 | +## Controller Integration |
| 4 | + |
| 5 | +### From Request Data |
| 6 | + |
| 7 | +```php |
| 8 | +use App\Dto\UserDto; |
| 9 | +use Illuminate\Http\Request; |
| 10 | + |
| 11 | +class UserController extends Controller |
| 12 | +{ |
| 13 | + public function store(Request $request): JsonResponse |
| 14 | + { |
| 15 | + // From validated request |
| 16 | + $dto = new UserDto($request->validated()); |
| 17 | + |
| 18 | + // Or with ignoreMissing for partial updates |
| 19 | + $dto = UserDto::createFromArray($request->all(), ignoreMissing: true); |
| 20 | + |
| 21 | + return response()->json($dto->toArray()); |
| 22 | + } |
| 23 | +} |
| 24 | +``` |
| 25 | + |
| 26 | +### From Eloquent Models |
| 27 | + |
| 28 | +```php |
| 29 | +public function show(int $id): JsonResponse |
| 30 | +{ |
| 31 | + $user = User::findOrFail($id); |
| 32 | + $dto = new UserDto($user->toArray()); |
| 33 | + |
| 34 | + return response()->json($dto); |
| 35 | +} |
| 36 | +``` |
| 37 | + |
| 38 | +## Collection Factory |
| 39 | + |
| 40 | +The service provider automatically configures Laravel collections for DTO collection fields: |
| 41 | + |
| 42 | +```php |
| 43 | +// In your DTO with collection fields |
| 44 | +$activeRoles = $dto->getRoles()->filter(fn ($role) => $role->isActive()); |
| 45 | +$roleNames = $dto->getRoles()->pluck('name'); |
| 46 | +``` |
| 47 | + |
| 48 | +## Validation |
| 49 | + |
| 50 | +Validate DTOs using Laravel's validator: |
| 51 | + |
| 52 | +```php |
| 53 | +use Illuminate\Support\Facades\Validator; |
| 54 | + |
| 55 | +$dto = new UserDto($request->all(), ignoreMissing: true); |
| 56 | + |
| 57 | +$validator = Validator::make($dto->toArray(), [ |
| 58 | + 'name' => 'required|string|max:255', |
| 59 | + 'email' => 'required|email|unique:users', |
| 60 | +]); |
| 61 | + |
| 62 | +if ($validator->fails()) { |
| 63 | + return response()->json(['errors' => $validator->errors()], 422); |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +## Form Requests |
| 68 | + |
| 69 | +Combine with Form Requests for clean validation: |
| 70 | + |
| 71 | +```php |
| 72 | +// app/Http/Requests/StoreUserRequest.php |
| 73 | +class StoreUserRequest extends FormRequest |
| 74 | +{ |
| 75 | + public function rules(): array |
| 76 | + { |
| 77 | + return [ |
| 78 | + 'name' => 'required|string|max:255', |
| 79 | + 'email' => 'required|email|unique:users', |
| 80 | + ]; |
| 81 | + } |
| 82 | + |
| 83 | + public function toDto(): UserDto |
| 84 | + { |
| 85 | + return new UserDto($this->validated()); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// In controller |
| 90 | +public function store(StoreUserRequest $request): JsonResponse |
| 91 | +{ |
| 92 | + $dto = $request->toDto(); |
| 93 | + // ... |
| 94 | +} |
| 95 | +``` |
| 96 | + |
| 97 | +## API Resources |
| 98 | + |
| 99 | +Use DTOs with API Resources: |
| 100 | + |
| 101 | +```php |
| 102 | +// app/Http/Resources/UserResource.php |
| 103 | +class UserResource extends JsonResource |
| 104 | +{ |
| 105 | + public function toArray($request): array |
| 106 | + { |
| 107 | + $dto = new UserDto($this->resource->toArray()); |
| 108 | + return $dto->toArray(); |
| 109 | + } |
| 110 | +} |
| 111 | +``` |
| 112 | + |
| 113 | +## Service Layer Pattern |
| 114 | + |
| 115 | +```php |
| 116 | +// app/Services/UserService.php |
| 117 | +class UserService |
| 118 | +{ |
| 119 | + public function createUser(UserDto $dto): User |
| 120 | + { |
| 121 | + return User::create($dto->toArray()); |
| 122 | + } |
| 123 | + |
| 124 | + public function updateUser(User $user, UserDto $dto): User |
| 125 | + { |
| 126 | + $user->update($dto->toArray()); |
| 127 | + return $user->fresh(); |
| 128 | + } |
| 129 | +} |
| 130 | +``` |
| 131 | + |
| 132 | +## Nested DTOs |
| 133 | + |
| 134 | +When your DTO has nested DTO fields: |
| 135 | + |
| 136 | +```php |
| 137 | +// config/dtos.xml |
| 138 | +<dto name="Order"> |
| 139 | + <field name="id" type="int"/> |
| 140 | + <field name="customer" type="Customer"/> |
| 141 | + <field name="items" type="OrderItem[]"/> |
| 142 | +</dto> |
| 143 | + |
| 144 | +// Usage |
| 145 | +$order = new OrderDto([ |
| 146 | + 'id' => 1, |
| 147 | + 'customer' => ['name' => 'John', 'email' => 'john@example.com'], |
| 148 | + 'items' => [ |
| 149 | + ['product' => 'Widget', 'quantity' => 2], |
| 150 | + ['product' => 'Gadget', 'quantity' => 1], |
| 151 | + ], |
| 152 | +]); |
| 153 | + |
| 154 | +// Access nested data |
| 155 | +$customerName = $order->getCustomer()->getName(); |
| 156 | +$totalItems = $order->getItems()->count(); |
| 157 | +``` |
| 158 | + |
| 159 | +## Further Reading |
| 160 | + |
| 161 | +See the main [php-collective/dto documentation](https://github.com/php-collective/dto) for: |
| 162 | +- DTO configuration options |
| 163 | +- Type support |
| 164 | +- Custom casters |
| 165 | +- Advanced patterns |
0 commit comments