From 6576d89d08775424eac20350bdbe37a47775048f Mon Sep 17 00:00:00 2001 From: kent Date: Mon, 28 Jul 2025 01:17:15 +0900 Subject: [PATCH] =?UTF-8?q?fix=20:=20=EC=B9=B4=ED=85=8C=EA=B3=A0=EB=A6=AC?= =?UTF-8?q?=20=ED=99=95=EC=9D=B8=20API=20=EC=88=98=EC=A0=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Services/ProductService.php | 35 ++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/app/Services/ProductService.php b/app/Services/ProductService.php index 3a3aa73..a4971f6 100644 --- a/app/Services/ProductService.php +++ b/app/Services/ProductService.php @@ -9,12 +9,41 @@ class ProductService { /** - * 회원 조회(리스트) + * 카테고리 트리 전체 조회 (parent_id = null 기준) */ - public static function getCategory() + public static function getCategory($request) { - $query = CommonCode::where('code_group','category')->where('parent_id',null); + $parentId = $request->parentId ?? null; + $group = $request->group ?? 'category'; + // 재귀적으로 트리 구성 + $list = self::fetchCategoryTree($parentId, $group); + + return ApiResponse::response('result', $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 ApiResponse::response('get', $query); }