Commit 05a7cf8b authored by Bogdan Stănescu's avatar Bogdan Stănescu Committed by Jens Segers

Fix attribute read for values nested on multiple levels (#1069)

* Add test for multiple level nested dot notation

* Fix multiple level attributes using dot notation

* Mantain parent logic on attribute read for non dot keys
parent dca0a9db
...@@ -239,11 +239,7 @@ abstract class Model extends BaseModel ...@@ -239,11 +239,7 @@ abstract class Model extends BaseModel
{ {
// Support keys in dot notation. // Support keys in dot notation.
if (str_contains($key, '.')) { if (str_contains($key, '.')) {
$attributes = array_dot($this->attributes); return array_get($this->attributes, $key);
if (array_key_exists($key, $attributes)) {
return $attributes[$key];
}
} }
return parent::getAttributeFromArray($key); return parent::getAttributeFromArray($key);
......
...@@ -495,6 +495,22 @@ class ModelTest extends TestCase ...@@ -495,6 +495,22 @@ class ModelTest extends TestCase
$this->assertEquals('Strasbourg', $user['address.city']); $this->assertEquals('Strasbourg', $user['address.city']);
} }
public function testMultipleLevelDotNotation()
{
$book = Book::create([
'title' => 'A Game of Thrones',
'chapters' => [
'one' => [
'title' => 'The first chapter',
],
],
]);
$this->assertEquals(['one' => ['title' => 'The first chapter']], $book->chapters);
$this->assertEquals(['title' => 'The first chapter'], $book['chapters.one']);
$this->assertEquals('The first chapter', $book['chapters.one.title']);
}
public function testGetDirtyDates() public function testGetDirtyDates()
{ {
$user = new User(); $user = new User();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment