Attributes Overview
All behaviour in Simple Data Objects is declared via PHP attributes on constructor parameters (or on the class itself for #[TransformKeys]/#[Pipe]/#[Discriminator]/#[RejectUnknownKeys]/#[InferRules], or on a method for #[Computed]). Every parameter-level attribute also works on a plain property for constructor-less and hybrid DTOs — same syntax, same behavior either way.
| Attribute | Target | Purpose |
|---|---|---|
#[Cast] | Parameter or property | Apply a type cast during hydration and serialization |
#[Rules] | Parameter or property | Laravel validation rules |
#[InferRules] | Class | Auto-generate validation rules from property types |
#[Flatten] | Parameter or property | Inline nested DTO fields into the parent array |
#[Hidden] | Parameter or property | Exclude from toArray() / JSON output |
#[IgnoreIfNull] | Parameter or property | Omit from output when value is null |
#[Computed] | Method | Add a derived, method-backed field to serialization output |
#[MapPropertyName] | Parameter or property | Map a different input key (or several aliases) to this property |
#[MapInputName] / #[MapOutputName] | Parameter or property | Map hydration and serialization keys independently |
#[TransformKeys] | Class | Transform all input keys at the class level |
#[Discriminator] | Class (abstract) | Polymorphic hydration — dispatch from() to a concrete subclass by field value |
#[DataCollection] | Parameter or property | Declare a typed collection property |
#[Pipe] | Class, parameter, or property | Input preprocessing middleware — whole array (class) or a single value (parameter/property) |
#[WhenLoaded] | Parameter or property | Include an Eloquent relation in fromModel() only when it's loaded |
#[RejectUnknownKeys] | Class | Strict mode — throw when input contains a key the class doesn't recognize |
#[WrapIn] | Class | Wrap toResponse()'s payload under a key (toArray()/toJson() unaffected) |