Introduction
bradietilley/laravel-data is a small, opinionated Data Transfer Object library.
A Data object is a plain PHP class with a typed constructor. This package adds
reflection-driven hydration (build an instance from an array, model, another
DTO, stdClass or JSON) and serialization (toArray()), plus a handful of
attributes for the cases that need more than a straight property copy.
Design goals
- Lightweight. No heavy runtime, no config to publish, no service provider.
The core is a single abstract
Dataclass plus a reflection cache. - Framework-agnostic core.
Data::from()/toArray()work with zero Laravel installed. Laravel features (Eloquent model hydration,Collectionresults,Carboncasting, paginator wrapping) light up automatically when the relevant packages are present — see Framework-agnostic usage. - Typed constructors as the source of truth. Your DTO is just its constructor signature. Named + typed arguments give you IDE autocomplete and static analysis for free; the library fills them from whatever input you hand it.
What it is not
- It is not a validation library. Hydration trusts its input; validate before or after as you see fit.
- It does not export TypeScript. That concern lives in laravel-suite — this package focuses on the PHP DTO runtime and PHP code generation.
When to use it
Reach for a Data object whenever you want a typed, self-describing payload: controller input/output, service boundaries, queue job payloads, API responses, or anywhere an untyped array would otherwise leak through your app.