Skip to content

Instantly share code, notes, and snippets.

@fatgy
Last active August 29, 2015 14:13
Show Gist options
  • Save fatgy/340c4544d62dc7b69fae to your computer and use it in GitHub Desktop.
Save fatgy/340c4544d62dc7b69fae to your computer and use it in GitHub Desktop.
traversal baum collection
function render($html, $node)
{
if($node->isRoot()) $html .= '<ul>';
$html .= '<li>' . $node->name;
if($node->children->count() > 0)
{
$html .= '<ul>';
$html .= $node->children->reduce(render);
$html .= '</ul>';
}
$html .= '</li>';
if($node->isRoot()) $html .= '</ul>';
return $html;
}
$tree = Category::all()->toHierarchy();
echo $tree->reduce(render);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment