Created
January 23, 2018 14:12
-
-
Save sobchenyuk/7b7be00a173d053aa1604ec93cadefc0 to your computer and use it in GitHub Desktop.
дерево категории LARAVEL \ the categories tree LARAVEL
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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