File tree Expand file tree Collapse file tree 2 files changed +33
-1
lines changed
src/Illuminate/Database/Eloquent/Concerns Expand file tree Collapse file tree 2 files changed +33
-1
lines changed Original file line number Diff line number Diff line change @@ -1271,7 +1271,11 @@ protected function asJson($value)
1271
1271
*/
1272
1272
public function fromJson ($ value , $ asObject = false )
1273
1273
{
1274
- return Json::decode ($ value ?? '' , ! $ asObject );
1274
+ if ($ value === null || $ value === '' ) {
1275
+ return null ;
1276
+ }
1277
+
1278
+ return Json::decode ($ value , ! $ asObject );
1275
1279
}
1276
1280
1277
1281
/**
Original file line number Diff line number Diff line change @@ -21,6 +21,14 @@ public function testWithConstructorArguments()
21
21
$ attributes = $ instance ->getMutatedAttributes ();
22
22
$ this ->assertEquals (['some_attribute ' ], $ attributes );
23
23
}
24
+
25
+ public function testCastingEmptyStringToArrayDoesNotError ()
26
+ {
27
+ $ instance = new HasAttributesWithArrayCast ();
28
+ $ this ->assertEquals (['foo ' => null ], $ instance ->attributesToArray ());
29
+
30
+ $ this ->assertTrue (json_last_error () === JSON_ERROR_NONE );
31
+ }
24
32
}
25
33
26
34
class HasAttributesWithoutConstructor
@@ -40,3 +48,23 @@ public function __construct($someValue)
40
48
{
41
49
}
42
50
}
51
+
52
+ class HasAttributesWithArrayCast
53
+ {
54
+ use HasAttributes;
55
+
56
+ public function getArrayableAttributes (): array
57
+ {
58
+ return ['foo ' => '' ];
59
+ }
60
+
61
+ public function getCasts (): array
62
+ {
63
+ return ['foo ' => 'array ' ];
64
+ }
65
+
66
+ public function usesTimestamps (): bool
67
+ {
68
+ return false ;
69
+ }
70
+ }
You can’t perform that action at this time.
0 commit comments