Files
sam-api/app/Services/ProductService.php

52 lines
1.3 KiB
PHP
Raw Normal View History

2025-07-23 18:06:33 +09:00
<?php
namespace App\Services;
use App\Helpers\ApiResponse;
use App\Models\Products\CommonCode;
class ProductService
{
/**
2025-07-28 01:17:15 +09:00
* 카테고리 트리 전체 조회 (parent_id = null 기준)
2025-07-23 18:06:33 +09:00
*/
2025-07-28 01:17:15 +09:00
public static function getCategory($request)
2025-07-23 18:06:33 +09:00
{
2025-07-28 01:17:15 +09:00
$parentId = $request->parentId ?? null;
$group = $request->group ?? 'category';
2025-07-23 18:06:33 +09:00
2025-07-28 01:17:15 +09:00
// 재귀적으로 트리 구성
$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);
2025-07-23 18:06:33 +09:00
}
}