是一个将数据格式化为树形结构的类库
执行命令:
composer require scolib/tree
或 在 composer.json 文件中:
"require": {
"scolib/tree": "^1.0"
}
并执行 composer update
<?php
namespace App\Repositories;
use Sco\Tree\Traits\TreeTrait;
class MyClass
{
use TreeTrait;
// 自定义属性(可选)
protected $treeNodeIdName = 'id';
protected $treeNodeParentIdName = 'parent_id';
protected $treeSpacer = ' ';
protected $treeFirstIcon = ' │ ';
protected $treeMiddleIcon = ' ├─ ';
protected $treeLastIcon = ' └─ ';
/**
* 获取待处理的原始节点数据
*
* 必须实现
*
* return \Illuminate\Support\Collection
*/
public function getTreeAllNodes()
{
}
}
public function setAllNodes(Collection $nodes)
public function getSubLevel($parentId)
public function getDescendants($parentId, $depth = 0, $adds = '')
public function getLayerOfDescendants($id)
public function getSelf($id)
public function getParent($id)
public function getAncestors($id, $depth = 0)