I want to have a eloquent value casted to an array and use Attribute get at the same time to set a value depending on the results. Otherwise to just return the casted value as before
protected $casts = [
'list' => 'array'
]
protected function list(): Attribute
{
// List might not be an array so force it become one
return Attribute::make(
get: fn ($value) => empty($value) ? [] : $value,
);
}
My list value could be something like false, 0, null and I want to convert it into an empty array before using it anywhere else in the laravel app. Otherwise it should continue being casted to an array. Or it being already casted, as false, 0, null would not be casted to an empty array without an attribute set.
It looks like the Attribute get actually overrides the cast itself. Where the $value is a raw value from the database and what you return is the final value. Of course I could transform the value into an array in the attribute itself, but I want use the casts as that makes things easier to maintain.
Is this possible?