程式碼:
public function getMenuTree($parentId = 0, $allMenus = null)
{
if (is_null($allMenus)) {
return [];
}
return $allMenus
->where('parent_id', $parentId)
->map(function (SysPermission $menu) use ($allMenus) {
$data = [
'id' => $menu->id,
'name' => $menu->name,
'path' => $menu->path,
'component' => $menu->component,
];
if ($menu->type !== SysPermission::TYPE_DIRECTORY) {
return $data;
}
$data['children'] = $this->getMenuTree($menu->id, $allMenus)->toArray();
return $data;
});
}
資料集測試列印:
用 Illuminate\Http\Resources\Json\JsonResource
則可以
本作品採用《CC 協議》,轉載必須註明作者和本文連結