NestedModelTrait::loadTree()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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