- How do I replace Laravel Form Request validation with this package in my controllers?
- Replace `validate()` calls in controllers with DTO injection. For example, inject `UserDTO $dto` into your controller method, and the package automatically validates the incoming request data against your DTO’s rules. This eliminates repetitive validation code while keeping the same validation logic.
- Does this package support nested DTOs for complex payloads like arrays of objects?
- Yes, you can nest DTOs by defining properties as typed arrays or other DTO classes. For example, `public array<CreateOrderItemDTO> $items;` will validate each item in the array. Use the `SkipOnTransform` attribute for nested objects that don’t need validation.
- What Laravel versions does `wendelladriel/laravel-validated-dto` support?
- The package supports Laravel 11+ and requires PHP 8.1+. Check the [Laravel Cloud badge](https://badge.laravel.cloud/badge/wendelladriel/laravel-validated-dto) for the latest compatibility details. Breaking changes (e.g., dropping Laravel 10 in v4.0.0) are documented in the changelog.
- Can I use this package for CLI commands or non-HTTP input sources?
- Absolutely. The package works with arrays, JSON, or any input source by manually instantiating the DTO with `new UserDTO($data)`. For CLI commands, bind the DTO to your command’s `handle()` method, and it will validate the input automatically.
- How does performance compare to manual validation in controllers?
- Validation adds minimal overhead, but the package optimizes performance with lazy validation (v3.10+). For high-frequency endpoints, ensure you’re not over-validating trivial fields. Benchmark critical paths if needed, but most use cases see negligible impact.
- Will this package work with existing Eloquent model casts or custom validation logic?
- Yes, you can reuse Eloquent model casts by defining them in your DTO’s `casts()` method. For custom validation logic, extend the `rules()` method or use Laravel’s validation rule objects (e.g., `Password::min(8)`). The package integrates seamlessly with Laravel’s validation ecosystem.
- How do I generate TypeScript types from my DTOs for frontend consistency?
- Use the built-in TypeScript definitions (added in v4.1.0) by running `php artisan make:dto --typescript`. This generates `.d.ts` files for your DTOs, ensuring type safety between your Laravel backend and frontend (e.g., Inertia.js or API clients).
- Can I customize DTO behavior, like adding hooks after validation?
- Yes, override methods like `afterValidation()` to run custom logic post-validation. You can also extend the `ValidatedDTO` class or use traits to add reusable behavior. For example, trigger events or transform data before it’s used in your business logic.
- What’s the best way to test DTOs in PHPUnit?
- Use the `DTOTestCase` provided by the package to simplify testing. It includes helpers for validating DTOs with mock data, checking defaults, and asserting validation errors. Example: `$dto = new UserDTO(['email' => 'invalid']); $this->assertValidationFails($dto);`.
- Should I replace all Form Requests with DTOs, or use them selectively?
- Start with non-critical endpoints (e.g., admin panels, internal APIs) to pilot the package. Replace Form Requests incrementally—DTOs excel for API payloads, while Form Requests may still suit web form submissions. Use both for a balanced migration path.