Skip to content

Commit

Permalink
Return null instead of empty array object or collection for non-array
Browse files Browse the repository at this point in the history
  • Loading branch information
a-bashtannik committed Apr 4, 2022
1 parent e5646d9 commit 156773c
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Eloquent/Casts/AsArrayObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function get($model, $key, $value, $attributes)
{
$data = json_decode($attributes[$key], true);

return is_array($data) ? new ArrayObject($data) : new ArrayObject();
return is_array($data) ? new ArrayObject($data) : null;
}

public function set($model, $key, $value, $attributes)
Expand Down
2 changes: 1 addition & 1 deletion src/Illuminate/Database/Eloquent/Casts/AsCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function get($model, $key, $value, $attributes)
{
$data = json_decode($attributes[$key], true);

return is_array($data) ? new Collection($data) : new Collection();
return is_array($data) ? new Collection($data) : null;
}

public function set($model, $key, $value, $attributes)
Expand Down
2 changes: 2 additions & 0 deletions tests/Integration/Database/DatabaseCustomCastsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,11 @@ public function test_custom_casting_nullable_values()
$this->assertEmpty($model->collection);
$this->assertSame('', (string) $model->stringable);

$model->array_object = ['name' => 'John'];
$model->array_object['name'] = 'Taylor';
$model->array_object['meta']['title'] = 'Developer';

$model->array_object_json = ['name' => 'John'];
$model->array_object_json['name'] = 'Taylor';
$model->array_object_json['meta']['title'] = 'Developer';

Expand Down

0 comments on commit 156773c

Please sign in to comment.