Test Setup Failed
Push — master ( c48a36...2ef870 )
by Oliver
04:00
created

NestedModelTrait::loadTree()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
1
<?php
2
3
4
namespace Webfactor\Laravel\Backpack\NestedModels\Traits;
5
6
7
use Kalnoy\Nestedset\Collection;
8
use Kalnoy\Nestedset\NodeTrait;
9
10
trait NestedModelTrait
11
{
12
    use NodeTrait;
13
14
    /**
15
     * Build the collection representing the tree with possibly many root nodes
16
     *
17
     * @param Collection|null $nodes
18
     * @return Collection
19
     */
20
    public static function loadTree(Collection $nodes = null)
21
    {
22
        $nodes = $nodes ?? static::all();
23
24
        return $nodes->where((new static)->getParentIdName(), null)
25
            ->values()
26
            ->each(function ($item) use ($nodes) {
27
                $nodes->toTree($item);
28
            });
29
    }
30
31
    /*
32
    |--------------------------------------------------------------------------
33
    | Kalnoy/NestedSet
34
    |--------------------------------------------------------------------------
35
    |
36
    | Overwrite the default tree columns here to fit with backpack reorder
37
    */
38
39
    public function getLftName()
40
    {
41
        return 'lft';
42
    }
43
44
    public function getRgtName()
45
    {
46
        return 'rgt';
47
    }
48
49
    public function getParentIdName()
50
    {
51
        return 'parent_id';
52
    }
53
}