FilamentF
Filament3y ago
1 reply
ericmp #2

How to cast properties of array recursively in a Laravel Model

inside my model i have this:
protected $casts = [
    'fields' => 'array',
];

where fields is an array like, stored in database as a json array:
[
    '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,
];

but it doesnt work

also tried:
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
Was this page helpful?