Files
sam-api/app/Services/ProductService.php
hskwon 43e4c507a7 fix : 결과 전달시 두번 래핑되는 부분 수정
- 컨트롤러와 서비스에서 각각 래핑 후 결과 전달됨으로 이중 래핑되고 있음
  -> 서비스에서 래핑하는 부분을 컨트롤러로 옮겨서 컨트롤러에서만 한번 래핑하는 걸로 수정
2025-08-19 12:41:17 +09:00

52 lines
1.3 KiB
PHP

<?php
namespace App\Services;
use App\Helpers\ApiResponse;
use App\Models\Products\CommonCode;
class ProductService
{
/**
* 카테고리 트리 전체 조회 (parent_id = null 기준)
*/
public static function getCategory($request)
{
$parentId = $request->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();
}
}