Fork Copy getall($menu_id); $groupedMenuList = $menu_list->groupBy('parent'); $roots = data_get($groupedMenuList, 0, []); $items = self::newTree($roots, $groupedMenuList); return $items; } private static function newTree($items, $groupedMenuList) { return collect($items)->map(function ($item) use ($groupedMenuList) { $menu = $item; $childs = data_get($groupedMenuList, $item->id, collect()); $menu['child'] = self::newTree($childs, $groupedMenuList); return $menu; }); } }