Skip to content

Commit

Permalink
Merge branch '10.x' into 11.x
Browse files Browse the repository at this point in the history
# Conflicts:
#	CHANGELOG.md
#	src/Illuminate/Foundation/Application.php
#	tests/Database/DatabaseConcernsHasAttributesTest.php
  • Loading branch information
driesvints committed Aug 9, 2024
2 parents e1a9c5d + 05a9554 commit 0e39947
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -1332,7 +1332,11 @@ protected function asJson($value)
*/
public function fromJson($value, $asObject = false)
{
return Json::decode($value ?? '', ! $asObject);
if ($value === null || $value === '') {
return null;
}

return Json::decode($value, ! $asObject);
}

/**
Expand Down
28 changes: 28 additions & 0 deletions tests/Database/DatabaseConcernsHasAttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ public function testRelationsToArray()
'null_relation' => null,
], $mock->relationsToArray());
}

public function testCastingEmptyStringToArrayDoesNotError()
{
$instance = new HasAttributesWithArrayCast();
$this->assertEquals(['foo' => null], $instance->attributesToArray());

$this->assertTrue(json_last_error() === JSON_ERROR_NONE);
}
}

class HasAttributesWithoutConstructor
Expand All @@ -60,3 +68,23 @@ public function __construct($someValue)
{
}
}

class HasAttributesWithArrayCast
{
use HasAttributes;

public function getArrayableAttributes(): array
{
return ['foo' => ''];
}

public function getCasts(): array
{
return ['foo' => 'array'];
}

public function usesTimestamps(): bool
{
return false;
}
}

0 comments on commit 0e39947

Please sign in to comment.