我有带有字段的类别模型:
id
name
parent_id
childrens
date
这里childrens
也是关系收集对象,我只能得到这样的必需字段:
$treeCategories = $category->map(function($cat) {
return [
'id' => $cat->id,
'name' => $cat->name,
'children' => $cat->children->map(function($child) {
return [
'id' => $child->id,
'name' => $child->name,
'children' => $child->map()
];
})->toArray()
];
})->toArray();
如果类别子类别也有孩子,该如何递归执行此操作?