From 3449c04133716cdbe65e01d21d0bb01962c93abe Mon Sep 17 00:00:00 2001 From: Bruno Bernard Date: Wed, 21 Aug 2024 12:07:35 +0400 Subject: [PATCH] Update docs to include nested property names --- .../mapping-property-names.md | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/docs/as-a-data-transfer-object/mapping-property-names.md b/docs/as-a-data-transfer-object/mapping-property-names.md index 6189b27cd..4a5cf8713 100644 --- a/docs/as-a-data-transfer-object/mapping-property-names.md +++ b/docs/as-a-data-transfer-object/mapping-property-names.md @@ -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"] + ] +]); +``` +