From 64f717a031dcc63429d357bceaf2cdc4b1905b98 Mon Sep 17 00:00:00 2001 From: Jonas Staudenmeir Date: Sun, 27 Oct 2024 11:56:50 +0100 Subject: [PATCH] Refactoring --- README.md | 11 ++++++----- src/Eloquent/Collection.php | 4 ++-- tests/Tree/CollectionTest.php | 12 ++++++------ 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index aad5416..6859c8e 100644 --- a/README.md +++ b/README.md @@ -176,20 +176,21 @@ $tree = User::tree(3)->get(); $tree = User::treeOf($constraint, 3)->get(); ``` -You can also chaperone tree relations to load parent/ancestor relations already present in the tree to (potentially) reduce 1+n queries: +You can also chaperone tree relationships to load `ancestors` and `parent` relationships already present in the tree to +(potentially) reduce N+1 queries: ```php -$tree = User::tree(3)->get(); +$users = User::tree(3)->get(); -$tree->loadTreePathRelations(); +$users->loadTreeRelationships(); ``` -Or via `toTree`: +Or with `toTree()`: ```php $users = User::tree(1)->get(); -$tree = $users->loadTreePathRelations()->toTree(); +$tree = $users->loadTreeRelationships()->toTree(); ``` #### Filters diff --git a/src/Eloquent/Collection.php b/src/Eloquent/Collection.php index f91ca8d..459d9ae 100644 --- a/src/Eloquent/Collection.php +++ b/src/Eloquent/Collection.php @@ -52,11 +52,11 @@ public function toTree($childrenRelation = 'children') } /** - * Load parent/ancestor relations already present in the tree. + * Load ancestor and parent relationships already present in the tree. * * @return static */ - public function loadTreePathRelations(): static + public function loadTreeRelationships(): static { if ($this->isEmpty()) { return $this; diff --git a/tests/Tree/CollectionTest.php b/tests/Tree/CollectionTest.php index b9995a0..77d8939 100644 --- a/tests/Tree/CollectionTest.php +++ b/tests/Tree/CollectionTest.php @@ -40,11 +40,11 @@ public function testToTreeWithEmptyCollection(): void $this->assertEmpty($tree); } - public function testLoadTreePathRelations(): void + public function testLoadTreeRelationships(): void { DB::enableQueryLog(); - $users = User::tree()->get()->loadTreePathRelations(); + $users = User::tree()->get()->loadTreeRelationships(); $this->assertCount(1, DB::getQueryLog()); @@ -59,11 +59,11 @@ public function testLoadTreePathRelations(): void } } - public function testLoadTreePathRelationsWithMissingModels(): void + public function testLoadTreeRelationshipsWithMissingModels(): void { DB::enableQueryLog(); - $users = User::tree()->where('id', '>', 5)->get()->loadTreePathRelations(); + $users = User::tree()->where('id', '>', 5)->get()->loadTreeRelationships(); $this->assertCount(2, DB::getQueryLog()); @@ -78,9 +78,9 @@ public function testLoadTreePathRelationsWithMissingModels(): void } } - public function testLoadTreePathRelationsWithEmptyCollection(): void + public function testLoadTreeRelationshipsWithEmptyCollection(): void { - $users = User::tree(1)->where('id', 0)->get()->loadTreePathRelations(); + $users = User::tree(1)->where('id', 0)->get()->loadTreeRelationships(); $this->assertEmpty($users); }