[Feature] Configurable Custom Serialization for Model #54115
-
When working with a CustomValueObject class Cast in a Laravel model, the value of the attribute is correctly transformed into a CustomValueObject instance as expected, and this behavior is perfect for internal usage. However, during serialization (toArray or toJson), I(may be We) do not want the attribute value to remain a CustomValueObject sometimes. Instead, I(may be We) want it to be converted to a different representation, such as a string, integer, or any other format derived from the CustomValueObject. To handle such scenarios, it would be beneficial to allow developers to define custom serialization behavior for model attributes. This could be achieved by introducing a method or configuration option, such as: public function serialization()
{
return [
'attribute_name' => fn($value) => $value->toDesiredFormat(),
];
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
After, surfing some time for this i got this Array / JSON Serialization for Value Object Casts... It's solves the problem.. But, although this can be a feature that make a way to modify value before serialization... |
Beta Was this translation helpful? Give feedback.
After, surfing some time for this i got this Array / JSON Serialization for Value Object Casts...
It's solves the problem.. But, although this can be a feature that make a way to modify value before serialization...