Skip to content

Instantly share code, notes, and snippets.

@sobchenyuk
Created January 23, 2018 14:12
Show Gist options
  • Save sobchenyuk/7b7be00a173d053aa1604ec93cadefc0 to your computer and use it in GitHub Desktop.
Save sobchenyuk/7b7be00a173d053aa1604ec93cadefc0 to your computer and use it in GitHub Desktop.
дерево категории LARAVEL \ the categories tree LARAVEL
$categorys = DB::table('category')
->orderBy('title', 'asc')
->get();
$tree = array();
foreach ($categorys as $row) {
$tree[(int)$row->parent_id][] = $row;
}
$traverse = function($tree, $prefix = '-', $pid=0) use (&$traverse) {
if (empty($tree[$pid]))
return;
foreach ($tree[$pid] as $k => $row) {
echo PHP_EOL.$prefix.' '.$row->title;
if (isset($tree[$row->id]))
$traverse($tree, $prefix.'-', $row->id );
}
};
$traverse($tree);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment