You are viewing the documentation for an older version of this package. You can check the version you are using with the following command:
composer show spatie/laravel-data
Mapping rules
It is possible to map the names properties going in and out of your data objects using: MapOutputName, MapInputName
and MapName attributes. But sometimes it can be quite hard to follow where which name can be used. Let's go through
some case:
In the data object:
classUserDataextendsData
{
publicfunction__construct(#[MapName('favorite_song')] // name mapping
publicLazy|SongData$song,
#[RequiredWith('song')] // In validation rules, use the original name
publicstring$title,
) {
}
publicstaticfunctionallowedRequestExcept(): ?array
{
return [
'song' // Use the original name when defining includes, excludes, excepts and only
];
}
// ...
}
When creating a data object:
UserData::from([
'favorite_song' => ..., // You can use the mapped or original name here
'title' => 'some title'
]);
When adding an include, exclude, except or only:
UserData::from(User::first())->except('song'); // Always use the original name here
Within a request query, you can use the mapped or original name: