如何可以递归映射laravel集合

时间:2019-06-04 05:00:40

标签: laravel eloquent

我有带有字段的类别模型:

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();

如果类别子类别也有孩子,该如何递归执行此操作?

0 个答案:

没有答案