Check this theme https://laracasts.com/discuss/channels/laravel/nested-category-depth
Jan 12, 2020
6
Level 1
How to display Categories as Tree hierarchical
I want to display the categories like this example :
So if a parent category have child .. display like :
Cars
Cars => Accessories
Electronics
Electronics => Laptop
Electronics => Laptop => Batteries
extra...
i have categories table which has parent_id column and every category can have a parent_id from it's parent category.
and of course i have a Model Category and Controller CategoryController
How can i achieve this?
Thanks
Level 61
Just plain recursive call should be sufficient here.
function printCategoryName($categories, $parentName = '') {
foreach ($categories as $category) {
$name = $parentName ? implode(' > ', [$parentName, $category->name]) : $category->name;
echo sprintf('%s%s', $name, PHP_EOL);
if (count($category->children) > 0) {
printCategoryName($category->children, $name);
}
}
}
printCategoryName($categories);
1 like
Please or to participate in this conversation.

