Skip to content

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.

AttributeTargetPurpose
#[Cast]Parameter or propertyApply a type cast during hydration and serialization
#[Rules]Parameter or propertyLaravel validation rules
#[InferRules]ClassAuto-generate validation rules from property types
#[Flatten]Parameter or propertyInline nested DTO fields into the parent array
#[Hidden]Parameter or propertyExclude from toArray() / JSON output
#[IgnoreIfNull]Parameter or propertyOmit from output when value is null
#[Computed]MethodAdd a derived, method-backed field to serialization output
#[MapPropertyName]Parameter or propertyMap a different input key (or several aliases) to this property
#[MapInputName] / #[MapOutputName]Parameter or propertyMap hydration and serialization keys independently
#[TransformKeys]ClassTransform all input keys at the class level
#[Discriminator]Class (abstract)Polymorphic hydration — dispatch from() to a concrete subclass by field value
#[DataCollection]Parameter or propertyDeclare a typed collection property
#[Pipe]Class, parameter, or propertyInput preprocessing middleware — whole array (class) or a single value (parameter/property)
#[WhenLoaded]Parameter or propertyInclude an Eloquent relation in fromModel() only when it's loaded
#[RejectUnknownKeys]ClassStrict mode — throw when input contains a key the class doesn't recognize
#[WrapIn]ClassWrap toResponse()'s payload under a key (toArray()/toJson() unaffected)

Released under the MIT License.