fix: Item API 응답에서 options를 최상위 레벨로 펼침
- formatItemResponse()에 flattenOptionsToResponse() 추가 - index(), show() 모두 동적 필드가 최상위 레벨로 출력 - options 배열 제거, label을 키로 value를 값으로 펼침
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user