laravel - 在 Laravel 8 中获取所有后代
问题描述
我正在尝试获取元素的后代(元素子、孙子、孙子)ID。
例如在控制器中,我正在调用这样的函数:
$categories = Equipment::find(1);
$ids = [];
$nestable = Equipment::nestable($ids,$categories);
和可嵌套功能:
public static function nestable($ids, $category)
{
if ($category->children->count() > 0) {
foreach ($category->children as $child) {
array_push($ids, $child->id);
$child = self::nestable($ids, $child);
}
}
return $ids;
}
但是方法只返回孩子,而不是返回孙子,但在方法中转储数组时,我得到了孩子和孙子。功能有什么问题?
解决方案
推荐阅读
- sql - 如何从 Microsoft SQL Server 中的字符串中修剪字符?
- python - 多个按钮在 PyQt5 python 中不起作用
- angular - 角度复选框状态检查经常触发
- powershell - 应该在 Assert-MockCalled 的 ParameterFilter 中不能按预期工作
- mysql - 更新查询不起作用 7-8% 的记录
- leaflet - 根据输入的地理编码位置,从数据库显示落在地图边界内的传单标记
- amazon-web-services - 为什么 ssh 通过 -i 标志而不是 -F 标志连接?
- android - android google play billing library v2.0.3 acknowledgePurchase failed with error code 5 in Android Q (api 29)
- android - 通过 Kotlin 中的类共享功能
- mysql - curdate() 的时间?