- How do I install laravel-validated-dto in a Laravel application?
- Install it with composer require wendelladriel/laravel-validated-dto. Optionally publish the config file with php artisan vendor:publish --tag="validated-dto". There are no migrations, queues, or external services to configure, so it works in an existing app immediately. Laravel package auto-discovery registers the service provider for you.
- Which Laravel and PHP versions does laravel-validated-dto support?
- Release lines track Laravel majors: v3.5.0 targets Laravel 11, v4.0.0 targets Laravel 12, and v4.5.0 targets Laravel 13. Check the badge and composer constraints for the PHP range of the line you pin. Because the package follows Laravel majors rather than independent semver, plan a package major bump in the same window as each framework upgrade. Add it to your platform upgrade runbook instead of treating it as an afterthought.
- What is the difference between a ValidatedDTO and a Laravel FormRequest?
- Both validate input, but a ValidatedDTO gives you a typed object with public typed properties instead of a bag of validated array data. You declare properties plus rules(), defaults(), and casts(), and the package hydrates, validates, and transforms the payload in one step. The key behavioral gap is authorization: FormRequest offers authorize(), while the DTO path pushes that responsibility to middleware and policies.
- How do I inject a DTO into a controller action?
- Type-hint the DTO in your controller method and the Laravel container resolves it from the current request automatically. That means you do not need to manually construct the DTO or call a factory in the action. Adoption stays per-endpoint and reversible, so you can migrate one route at a time without touching the rest of the app.
- Can I generate DTO classes with an artisan command?
- Yes, the package ships an artisan generator (make:dto) for scaffolding new DTO classes. Generated stubs are customizable, so you can bake in your team's conventions for namespaces, rules, defaults, and casts. Once the first DTO exists, the marginal cost of the tenth is close to zero.
- How do casts and property transformation work in ValidatedDTO?
- You define a casts() method using the package cast classes, such as StringCast and BooleanCast, to convert incoming values to the property types you declared. Transformation is the historically fragile surface of this package: changelogs show fixes for missing properties on transform, missing nested DTO properties, and toArray output. Pin versions and add contract tests asserting the exact toArray() shape you depend on.
- How do I handle nested DTOs and arrays of DTOs?
- Use DTOcast for nested DTO properties, and cast arrays of payloads into collections of DTOs where needed. Recent releases specifically fixed nested DTO property handling and interaction between DTOcast and Laravel's Cast attribute, so verify your nesting depth on the exact version you install. For polymorphic payloads, decide per endpoint whether to cast to concrete DTOs or leave the raw array and map it in your service layer.
- Can I build a DTO from an Eloquent model?
- Yes, the fromModel() helper hydrates a DTO from an Eloquent model, which is useful when returning typed objects from repositories or console commands. The package couples only to Illuminate\Validation and, through fromModel(), to Eloquent, so it does not force an architectural rewrite. Your domain services receive typed objects instead of raw arrays.
- How does laravel-validated-dto compare to Spatie Data or Laravel API Resources?
- ValidatedDTO is a focused request-to-domain boundary tool: it validates and hydrates typed objects, but it is not a full serialization or routing layer. If you need resource transformation, route-model binding, or versioned API contracts, keep Laravel API Resources alongside it. Compared with Spatie Data, expect a smaller, more opinionated surface with fewer serialization features.
- Is laravel-validated-dto safe to use in production and actively maintained?
- It is MIT licensed, has a small surface area, and roughly 774 stars with a single primary maintainer and a narrow contributor pool. Feature velocity is healthy but the API still moves in minor releases, so pin versions and contract-test your transform output. Treat the fork cost, roughly one engineer-week for the subset most teams use, as the real contingency rather than assuming upstream responsiveness, and verify activity on Packagist and the issue tracker before committing.