how can I show the whole tree. I have a 3 Level Navigation (First is root). With that code, I will only see the 2nd Level.
$tree = \App\Category::where('identifier', 'soccer')->first();
foreach($tree->getDescendants()->toHierarchy() as $descendant) {
echo "{$descendant->name} <br>";
}
You can get the whole tree including root by doing:
$root = Table::where('id', '=', $id)->first();
$tree = $root->getDescendantsAndSelf()->toHierarchy();
Now as $tree is Tree structure you need to traverse it recursively or with a queue data structure (DFS or BFS). Each item on the tree will have a children property with its children
Some pseudo for traversing would be:
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);
}
}
}