From f1975ee0ee2d80722d546167c879856917686584 Mon Sep 17 00:00:00 2001 From: hskwon Date: Tue, 16 Dec 2025 16:00:03 +0900 Subject: [PATCH] =?UTF-8?q?fix:=20Item=20API=20=EC=9D=91=EB=8B=B5=EC=97=90?= =?UTF-8?q?=EC=84=9C=20options=EB=A5=BC=20=EC=B5=9C=EC=83=81=EC=9C=84=20?= =?UTF-8?q?=EB=A0=88=EB=B2=A8=EB=A1=9C=20=ED=8E=BC=EC=B9=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - formatItemResponse()에 flattenOptionsToResponse() 추가 - index(), show() 모두 동적 필드가 최상위 레벨로 출력 - options 배열 제거, label을 키로 value를 값으로 펼침 --- app/Services/ItemService.php | 38 ++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) 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; }