Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
staudenmeir committed Oct 27, 2024
1 parent c47a8be commit 64f717a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/Eloquent/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions tests/Tree/CollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand All @@ -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());

Expand All @@ -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);
}
Expand Down

0 comments on commit 64f717a

Please sign in to comment.