public function index()
{
$categories = Categories::all()->all();
foreach ($categories as $category) {
if ($category->parent == 0) {
echo $category->name;
}
echo '<ul>';
Categories::getTree($categories, $category->categories_id);
echo '</ul>';
}
}
public static function getTree($categories, $parent_id)
{
for ($i = 0; $i < count($categories); $i++) {
if($categories[$i]->parent == $parent_id) {
echo '<li><a>' . $categories[$i]->name . '</a></li>';
}
self::getTree($categories, $categories[$i]->categories_id);
}
}
public function index()
{
$categories = ...;
echo self::getTree($categories);
}
public static function getTree($categories, $parent_id = 0) // или null, смотря что по дефолту
{
$result = '';
foreach ($categories as $category) {
if ($category->parent != $parent_id) {
continue;
}
$result .= sprintf('<li><a>%s</a>%s</li>',
$category->name,
self::getTree($categories, $category->categories_id) // или $category->id, смотря где там id
);
}
return $result ? sprintf('<ul>%s</ul>', $result) : '';
}