Replies: 1 comment 1 reply
-
Hi. Attributes that are null will not be cast. Also, json_decode('', true) returns null. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Laravel Version
10.10
PHP Version
8.2.12
Database Driver & Version
MySQL
Description
Dear Laravel Development Team,
I hope this message finds you well. I am writing to bring attention to an issue I have encountered with the $casts functionality in Laravel, specifically when dealing with empty JSON fields. Currently, when a JSON field is empty, the $casts attribute seems to be returning null instead of an empty array, which is the expected behavior when casting to an array.
Issue Description:
When utilizing $casts in a Laravel model to cast a JSON field to an array, the expected behavior is that an empty JSON field should be cast to an empty array. However, in the current implementation, an empty JSON field results in the casted attribute being set to null instead of an empty array.
Example:
Consider the following model definition:
If the 'json_field' is empty in the database, accessing it on an instance of ExampleModel would ideally return an empty array. However, it currently returns null.
Steps To Reproduce
Create a model with a JSON field.
Set $casts for the JSON field to 'array'.
Save an instance of the model with an empty JSON field.
Retrieve the model instance and access the casted attribute.
Expected Result:
The casted attribute should be an empty array.
Actual Result:
The casted attribute is currently set to null.
Beta Was this translation helpful? Give feedback.
All reactions