Skip to content

Commit

Permalink
Merge pull request #850 from eznix86/patch-1
Browse files Browse the repository at this point in the history
Update docs to include nested property names
  • Loading branch information
rubenvanassche committed Aug 21, 2024
2 parents 16a842a + 3449c04 commit f8199d3
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions docs/as-a-data-transfer-object/mapping-property-names.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,35 @@ class ContractData extends Data
}
}
```


## Mapping Nested Properties

You can also map nested properties using dot notation in the `MapInputName` attribute. This is useful when you want to extract a nested value from an array and assign it to a property in your data object:

```php
class SongData extends Data
{
public function __construct(
#[MapInputName("title.name")]
public string $title,
#[MapInputName("artists.0.name")]
public string $artist
) {
}
}
```

You can create the data object from an array with nested structures:

```php
SongData::from([
"title" => [
"name" => "Never gonna give you up"
],
"artists" => [
["name" => "Rick Astley"]
]
]);
```

0 comments on commit f8199d3

Please sign in to comment.