Skip to content

Commit 05a7cf8

Browse files
bstanescujenssegers
authored andcommitted
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
1 parent dca0a9d commit 05a7cf8

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

src/Jenssegers/Mongodb/Eloquent/Model.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -239,11 +239,7 @@ protected function getAttributeFromArray($key)
239239
{
240240
// Support keys in dot notation.
241241
if (str_contains($key, '.')) {
242-
$attributes = array_dot($this->attributes);
243-
244-
if (array_key_exists($key, $attributes)) {
245-
return $attributes[$key];
246-
}
242+
return array_get($this->attributes, $key);
247243
}
248244

249245
return parent::getAttributeFromArray($key);

tests/ModelTest.php

+16
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,22 @@ public function testDotNotation()
495495
$this->assertEquals('Strasbourg', $user['address.city']);
496496
}
497497

498+
public function testMultipleLevelDotNotation()
499+
{
500+
$book = Book::create([
501+
'title' => 'A Game of Thrones',
502+
'chapters' => [
503+
'one' => [
504+
'title' => 'The first chapter',
505+
],
506+
],
507+
]);
508+
509+
$this->assertEquals(['one' => ['title' => 'The first chapter']], $book->chapters);
510+
$this->assertEquals(['title' => 'The first chapter'], $book['chapters.one']);
511+
$this->assertEquals('The first chapter', $book['chapters.one.title']);
512+
}
513+
498514
public function testGetDirtyDates()
499515
{
500516
$user = new User();

0 commit comments

Comments
 (0)