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:
class UserData extends Data
public function __construct(
#[MapName('favorite_song')] // name mapping
public Lazy|SongData $song,
#[RequiredWith('song')] // In validation rules, use the original name
public string $title,
public static function allowedRequestExcept(): ?array
return [
'song',
public function rules(ValidContext $context): array {
return [
'song' => 'required',
When creating a data object:
UserData::from([
'favorite_song' => ...,
'title' => 'some title'
When adding an include, exclude, except or only:
UserData::from(User::first())->except('song');
Within a request query, you can use the mapped or original name:
https://spatie.be/my-account?except[]=favorite_song
When validating a data object or getting rules for a data object, always use the original name:
$data = [
'favorite_song' => 123,
'title' => 'some title',
UserData::validate($data)
UserData::getValidationRules($data)