From 404635500028609c011d89bf65d435e804d7db3c Mon Sep 17 00:00:00 2001 From: Jonas Staudenmeir Date: Wed, 18 Aug 2021 09:27:42 +0200 Subject: [PATCH] Fix recursion issues with JSON encoding of toTree() results --- README.md | 26 +++++++++++--------------- src/Eloquent/Collection.php | 7 +------ tests/CollectionTest.php | 6 ------ 3 files changed, 12 insertions(+), 27 deletions(-) diff --git a/README.md b/README.md index 36221c2..98b7caa 100644 --- a/README.md +++ b/README.md @@ -339,7 +339,7 @@ $users = User::tree()->get(); $tree = $users->toTree(); ``` -This recursively sets `children` and `parent` relationships: +This recursively sets `children` relationships: ```json [ @@ -350,23 +350,19 @@ This recursively sets `children` and `parent` relationships: "id": 2, "children": [ { - "id": 4, - "children": [], - "parent": { - "id": 2 - } + "id": 3, + "children": [] } - ], - "parent": { - "id": 1 - } + ] }, { - "id": 3, - "children": [], - "parent": { - "id": 1 - } + "id": 4, + "children": [ + { + "id": 5, + "children": [] + } + ] } ] } diff --git a/src/Eloquent/Collection.php b/src/Eloquent/Collection.php index 6563d1b..db46961 100644 --- a/src/Eloquent/Collection.php +++ b/src/Eloquent/Collection.php @@ -6,7 +6,7 @@ class Collection extends Base { - public function toTree($childrenRelation = 'children', $parentRelation = 'parent') + public function toTree($childrenRelation = 'children') { if ($this->isEmpty()) { return $this; @@ -23,14 +23,9 @@ public function toTree($childrenRelation = 'children', $parentRelation = 'parent ); $itemsByParentKey = $this->groupBy($parentKeyName); - $itemsByLocalKey = $this->keyBy($localKeyName); foreach ($this->items as $item) { $item->setRelation($childrenRelation, $itemsByParentKey[$item->$localKeyName] ?? new static()); - - if (isset($item->$parentKeyName, $itemsByLocalKey[$item->$parentKeyName])) { - $item->setRelation($parentRelation, $itemsByLocalKey[$item->$parentKeyName]); - } } return $tree; diff --git a/tests/CollectionTest.php b/tests/CollectionTest.php index cf2a11a..2b29a10 100644 --- a/tests/CollectionTest.php +++ b/tests/CollectionTest.php @@ -13,10 +13,7 @@ public function testToTree() $tree = $users->toTree(); $this->assertEquals([1, 11], $tree->pluck('id')->all()); - $this->assertFalse($tree[0]->relationLoaded('parent')); $this->assertEquals([2, 3, 4], $tree[0]->children->pluck('id')->all()); - $this->assertTrue($tree[0]->children[0]->relationLoaded('parent')); - $this->assertEquals(1, $tree[0]->children[0]->parent->id); $this->assertEquals([5], $tree[0]->children[0]->children->pluck('id')->all()); $this->assertEquals([8], $tree[0]->children[0]->children[0]->children->pluck('id')->all()); $this->assertEquals([12], $tree[1]->children->pluck('id')->all()); @@ -29,10 +26,7 @@ public function testToTreeWithDescendants() $tree = $users->toTree(); $this->assertEquals([2, 3, 4], $tree->pluck('id')->all()); - $this->assertFalse($tree[0]->relationLoaded('parent')); $this->assertEquals([5], $tree[0]->children->pluck('id')->all()); - $this->assertTrue($tree[0]->children[0]->relationLoaded('parent')); - $this->assertEquals(2, $tree[0]->children[0]->parent->id); $this->assertEquals([8], $tree[0]->children[0]->children->pluck('id')->all()); }