DataCollection default to empty array value #336
Replies: 3 comments
-
Hmm such thing is not possible at the moment, one of my plans is to get completely rid of the DataCollection type in the future and just use arrays but that's something which will take a lot of time. For now, You could use Or create a custom pipe which checks each DataProperty if it is a data collection, if not set, an empty array can be set. |
Beta Was this translation helpful? Give feedback.
-
I'm also looking for similar behavior for this. I ended up doing like this.
Is this approach valid and does not have any side effects with other features such as using in resource and request validation? |
Beta Was this translation helpful? Give feedback.
-
I'm also wondering what's the best way to achieve it. In nested data classes, I feel like @Defrothew is an easier approach. But I'm not sure about the side effects 🤔 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi, I am migrating from Spatie\DataTransferObject to LaravelData, I'm having some trouble migrating one of our DTO's.
DataTransferObject had the ability to cast an array type's inner values to a class, using eg.
#[CastWith(ArrayCaster::class, itemType: ConditionData::class)]
, but the actual type remained asarray
. eg.public array $conditions = [];
But with DataCollection, I can not set the default value to
[]
as it's a mismatched type. I am wondering what the equivalent for that is in LaravelData - because I also can not simply= ConditionData::collection()
, as it's an expressionSnippet of DTO after migration
Old usage (which worked previously):
Ideal New usage (without
conditions
array passed, throws error):New usage without
conditions
array passed:Beta Was this translation helpful? Give feedback.
All reactions