Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Dto Laravel Package

romanzipp/dto

Lightweight data transfer objects for PHP and Laravel. Define typed properties, cast and validate values, and convert DTOs to and from arrays with ease, keeping request and response data structured and type-safe.

View on GitHub
Deep Wiki
Context7

A strongly typed Data Transfer Object without magic for PHP 7.4+

Frequently asked questions about Dto
How do I install romanzipp/dto in a Laravel application?
Install it with composer require romanzipp/dto and start defining subclasses that extend the AbstractData base class. Each public typed property becomes part of the DTO shape, and you can hydrate from an array or serialize back with toArray(). There is no service provider or configuration file to publish, so installation is a single Composer dependency with no Laravel wiring required.
Which PHP and Laravel versions does romanzipp/dto support?
The package description states PHP 7.4+, but the API surface relies on PHP 8 features such as typed properties, attributes, and constructor promotion. That documentation discrepancy should be verified against composer.json and the src directory before you commit to it. There is no published LTS matrix or CI badge visible, so test it against your exact Laravel and PHP combination in a branch first.
How do I build a DTO from a Laravel Form Request?
Keep validation in your Form Request, then pass $request->validated() into your DTO constructor or hydration method. The DTO becomes the typed boundary object you hand to an action or service, so the rest of the codebase never touches raw request arrays. Nested arrays are hydrated into nested DTOs automatically when the target property type is a DTO subclass.
How does validation work on a romanzipp/dto class?
Validation is attribute based: Required, Nullable, Between, and In can be attached to properties and are evaluated during hydration. The package deliberately does not replace Laravel validation, so the usual pattern is Form Request validation first, then DTO attribute checks as a second guarantee at the boundary. Confirm the failure behavior, meaning whether exceptions are thrown or aggregated, before relying on it in a request lifecycle.
Does romanzipp/dto handle nested DTOs and arrays of DTOs?
Nested DTOs are auto-hydrated from nested arrays when the property is typed to a DTO subclass. Recursive collection mapping into arrays of DTOs, such as AddressData[], is not something the package explicitly promises, so verify it in a small test before designing around it. If you need computed fields, conditional transforms, or deep collection mapping, a larger package is a better fit.
Can I convert a DTO back to an array or JSON response?
Yes, call toArray() on the DTO to get a plain array suitable for a JSON response or persistence layer. For API endpoints you can still return a Laravel API Resource and treat the DTO purely as the internal typed shape. Keep serialization conventions consistent across DTOs so responses do not drift from your resource classes.
Is romanzipp/dto a replacement for spatie/laravel-data?
No, the scope is much smaller and intentionally so. romanzipp/dto gives you a typed base class with hydration, attribute validation, and toArray(), while spatie/laravel-data adds transformation pipelines, nested collections, computed properties, and Eloquent integration. If you need those features, spatie/laravel-data is the better maintained answer and should be the explicit comparison in your decision record.
Is romanzipp/dto safe to use in production?
The runtime code is simple, but the ecosystem risk is real: roughly a dozen stars, zero dependents, and no visible security or LTS posture. Adopting it means your team effectively becomes the maintainer, so pin a version or vendor the source at a recorded commit. Because it is a small base class, a 50 to 100 line internal equivalent is a realistic fallback if the package stalls.
Does romanzipp/dto work well with PHPStan and Psalm at strict levels?
Typed properties give static analysers something concrete to work with, which is a clear improvement over untyped arrays. However, hydration returns and magic-free construction paths should be checked against your strict rule set before you rely on the package in analysed code. Run your existing PHPStan or Psalm configuration over one pilot DTO to confirm there are no surprises.
How should I introduce DTOs into an existing Laravel codebase?
Start with one bounded surface such as a single endpoint, a queue job payload, or a third-party API response mapper. Introduce the DTO between the Form Request and the action, leaving the rest of the application untouched. Because it is a reversible, single-sprint decision, define a success criterion up front and expand only if the boundary is measurably clearer.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
microsoft/fluentui-system-icons
mmccook/php-json-canonicalization-scheme
middlewares/firewall
qubiqx/montapacking
nyoncode/laravel-package-console
pnscripts/filament-broadquill-lite
pnscripts/laravel-sentfold-lite
vleap/mx-sdk-laravel-internal
twidpay/utils
htetmyathlaing/scout-elasticsearch-driver
artisan-toolbox/front-awesome
ls/client-assistant
users-au/laravel-client
cgv/localized-resource
noith/settings
kda/laravel-package
kda/filament-dynamic-navigation
bypersoft/signbox
pairus/product-data
fayne/laravel-filament-cms-core