-
-
Notifications
You must be signed in to change notification settings - Fork 111
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8e680c9
commit 6edd164
Showing
4 changed files
with
141 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Staudenmeir\LaravelAdjacencyList\Eloquent; | ||
|
||
use Illuminate\Database\Eloquent\Collection as Base; | ||
|
||
class Collection extends Base | ||
{ | ||
public function toTree($childrenRelation = 'children', $parentRelation = 'parent') | ||
{ | ||
if ($this->isEmpty()) { | ||
return $this; | ||
} | ||
|
||
$parentKeyName = $this->first()->getParentKeyName(); | ||
$localKeyName = $this->first()->getLocalKeyName(); | ||
$depthName = $this->first()->getDepthName(); | ||
|
||
$depths = $this->pluck($depthName); | ||
|
||
$tree = new static( | ||
$this->where($depthName, $depths->min())->values() | ||
); | ||
|
||
$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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace Tests; | ||
|
||
use Tests\Models\User; | ||
|
||
class CollectionTest extends TestCase | ||
{ | ||
public function testToTree() | ||
{ | ||
$users = User::tree()->orderBy('id')->get(); | ||
|
||
$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()); | ||
} | ||
|
||
public function testToTreeWithDescendants() | ||
{ | ||
$users = User::find(1)->descendants()->orderBy('id')->get(); | ||
|
||
$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()); | ||
} | ||
|
||
public function testToTreeWithEmptyCollection() | ||
{ | ||
$users = User::tree(1)->where('id', 0)->get(); | ||
|
||
$tree = $users->toTree(); | ||
|
||
$this->assertEmpty($tree); | ||
} | ||
} |