我有 2 个具有以下结构的表
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 |
我正在寻找一种方法来获取所有 parent 及其 child ,然后将子 child 放入数组中。层次结构可以是无限的。
除了循环直到 n
之外,还有其他方法吗?
最佳答案
我想你会需要这个包。无需使用递归
https://github.com/lazychaser/laravel-nestedset
//访问祖先
$node->ancestors;
//访问后代
$node->descendants;
问题很容易解决
关于php - Laravel-查询获取父级和所有可能的子级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51722589/