diff --git a/app/Services/ItemService.php b/app/Services/ItemService.php index c0e5410..edd0548 100644 --- a/app/Services/ItemService.php +++ b/app/Services/ItemService.php @@ -342,7 +342,7 @@ public function index(array $params): LengthAwarePaginator $paginator = $query->orderBy('id')->paginate($size); - // 날짜 형식 변환 및 files 그룹화 + // 날짜 형식 변환, files 그룹화, options 펼침 $paginator->setCollection( $paginator->getCollection()->transform(function ($item) { $arr = $item->toArray(); @@ -353,6 +353,9 @@ public function index(array $params): LengthAwarePaginator // files를 field_key별로 그룹화 $arr['files'] = $this->groupFilesByFieldKey($arr['files'] ?? []); + // options를 최상위 레벨로 펼침 (동적 필드) + $arr = $this->flattenOptionsToResponse($arr); + return $arr; }) ); @@ -470,7 +473,7 @@ public function show(int $id, ?string $itemType = null): array } /** - * 품목 응답 포맷 (files 그룹화 포함) + * 품목 응답 포맷 (files 그룹화 + options 펼침) */ private function formatItemResponse(Model $item): array { @@ -484,6 +487,37 @@ private function formatItemResponse(Model $item): array // files를 field_key별로 그룹화 $arr['files'] = $this->groupFilesByFieldKey($arr['files'] ?? []); + // options를 최상위 레벨로 펼침 (동적 필드) + $arr = $this->flattenOptionsToResponse($arr); + + return $arr; + } + + /** + * options 배열을 최상위 레벨로 펼침 + * + * [{label: "field1", value: "val1"}, ...] → {"field1": "val1", ...} + */ + private function flattenOptionsToResponse(array $arr): array + { + $options = $arr['options'] ?? []; + unset($arr['options']); + + if (! is_array($options) || empty($options)) { + return $arr; + } + + // [{label, value, unit}] 형태의 배열을 펼침 + foreach ($options as $opt) { + if (isset($opt['label']) && $opt['label'] !== '') { + $key = $opt['label']; + // 기존 필드와 충돌 방지 + if (! isset($arr[$key])) { + $arr[$key] = $opt['value'] ?? ''; + } + } + } + return $arr; }