I have 2 tables with following structure
categories
id | name |
1 | Grocery |
2 | Beverages |
3 | Tea |
category_to_category
id | cat_id | parent_id |
1 | 1 | null |
2 | 2 | 1 |
3 | 3 | 2 |
I am looking for a way to get all the parents and their children and then sub children in an array. Hierarchy can be unlimited. Is there any other way instead of doing loops till n
?
I think you will need this package. No need to use recursion
https://github.com/lazychaser/laravel-nestedset
// Accessing ancestors
$node->ancestors;
// Accessing descendants
$node->descendants;
So easy to resolve problem