Skip to main content
The WithData trait enables support for the getData() method on Models, Requests, or any class that can be magically converted to a data object.

Using with Models

Add the trait and specify the data class:
Quickly get the data object:

Using with Form Requests

For FormRequests, use a method instead of a property:
Use in a controller:

Benefits

Clean Controllers

Reduce boilerplate by calling getData() instead of SongData::from($model)

Type Safety

The data class is defined once and reused consistently

Discoverability

IDEs can autocomplete the data class from the model/request

Validation

Form requests automatically validate before creating the data object

Complete Example

The getData() method respects all the same creation rules as calling SongData::from() directly, including magic methods and validation.