How to cast properties of array recursively in a Laravel Model

inside my model i have this:
protected $casts = [
'fields' => 'array',
];
protected $casts = [
'fields' => 'array',
];
where fields is an array like, stored in database as a json array:
[
'type' => 3,
...
]
[
'type' => 3,
...
]
where the type is an integer. this integer is a value from an enum so i try:
protected $casts = [
'fields' => 'array',
'fields.*.type' => FormFieldTypeEnum::class,
];
protected $casts = [
'fields' => 'array',
'fields.*.type' => FormFieldTypeEnum::class,
];
but it doesnt work also tried:
protected $casts = [
'fields' => 'array',
'fields.type' => FormFieldTypeEnum::class,
];
protected $casts = [
'fields' => 'array',
'fields.type' => FormFieldTypeEnum::class,
];
u know how to achieve it? also trying to understand it from the docs but not quite getting it - https://laravel.com/docs/10.x/eloquent-mutators#enum-casting
0 Replies
No replies yetBe the first to reply to this messageJoin

Did you find this page helpful?