parentId ?? null; $group = $request->group ?? 'category'; // 재귀적으로 트리 구성 $list = self::fetchCategoryTree($parentId, $group); return $list; } /** * 내부 재귀 함수 (하위 카테고리 트리 구조로 구성) */ protected static function fetchCategoryTree($parentId = null, $group = 'category') { $categories = CommonCode::where('code_group', 'category') ->where('parent_id', $parentId) ->orderBy('sort_order')->debug(); $categories = $categories->get(); foreach ($categories as $category) { $category->children = self::fetchCategoryTree($category->id); } return $categories; } /** * (예시) 기존의 flat 리스트 조회 */ public static function getCategoryFlat($group = 'category') { $query = CommonCode::where('code_group',$group)->whereNull('parent_id'); return $query->get(); } }