如何显示整棵树。我有一个3级导航(首先是root)。使用该代码,我只会看到第二级。
$tree = \App\Category::where('identifier', 'soccer')->first();
foreach($tree->getDescendants()->toHierarchy() as $descendant) {
echo "{$descendant->name} <br>";
}
答案 0 :(得分:0)
你可以通过以下方式获得包括root在内的整个树:
$root = Table::where('id', '=', $id)->first();
$tree = $root->getDescendantsAndSelf()->toHierarchy();
现在$ tree是Tree结构,你需要递归地遍历它或者使用队列数据结构(DFS或BFS)。树上的每个项目都有一个带有子项的子属性
用于遍历的一些伪将是:
function traverseBFS(tree) {
q = Queue()
q.push(tree[0]);
while (!q.empty()) {
item = q.top(); q.pop();
// do what you need with item
foreach (item->children as child) {
q.push(child);
}
}
}