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)
12711271 */
12721272 public function fromJson ($ value , $ asObject = false )
12731273 {
1274- return Json::decode ($ value ?? '' , ! $ asObject );
1274+ if ($ value === null || $ value === '' ) {
1275+ return null ;
1276+ }
1277+
1278+ return Json::decode ($ value , ! $ asObject );
12751279 }
12761280
12771281 /**
Original file line number Diff line number Diff line change @@ -21,6 +21,14 @@ public function testWithConstructorArguments()
2121 $ attributes = $ instance ->getMutatedAttributes ();
2222 $ this ->assertEquals (['some_attribute ' ], $ attributes );
2323 }
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+ }
2432}
2533
2634class HasAttributesWithoutConstructor
@@ -40,3 +48,23 @@ public function __construct($someValue)
4048 {
4149 }
4250}
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