Skip to content

Commit 156773c

Browse files
committed
Return null instead of empty array object or collection for non-array
1 parent e5646d9 commit 156773c

File tree

3 files changed

+4
-2
lines changed

3 files changed

+4
-2
lines changed

src/Illuminate/Database/Eloquent/Casts/AsArrayObject.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function get($model, $key, $value, $attributes)
2121
{
2222
$data = json_decode($attributes[$key], true);
2323

24-
return is_array($data) ? new ArrayObject($data) : new ArrayObject();
24+
return is_array($data) ? new ArrayObject($data) : null;
2525
}
2626

2727
public function set($model, $key, $value, $attributes)

src/Illuminate/Database/Eloquent/Casts/AsCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function get($model, $key, $value, $attributes)
2222
{
2323
$data = json_decode($attributes[$key], true);
2424

25-
return is_array($data) ? new Collection($data) : new Collection();
25+
return is_array($data) ? new Collection($data) : null;
2626
}
2727

2828
public function set($model, $key, $value, $attributes)

tests/Integration/Database/DatabaseCustomCastsTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,11 @@ public function test_custom_casting_nullable_values()
9898
$this->assertEmpty($model->collection);
9999
$this->assertSame('', (string) $model->stringable);
100100

101+
$model->array_object = ['name' => 'John'];
101102
$model->array_object['name'] = 'Taylor';
102103
$model->array_object['meta']['title'] = 'Developer';
103104

105+
$model->array_object_json = ['name' => 'John'];
104106
$model->array_object_json['name'] = 'Taylor';
105107
$model->array_object_json['meta']['title'] = 'Developer';
106108

0 commit comments

Comments
 (0)