Laravel은 API를 구축 할 때 기본적으로 리소스 컬렉션의 인덱스를 렌더링합니다. 원래 키에 의미가있는 상황의 경우
또 다른 예 : preserveKeys
이것은 다음과 같은 응답을 반환합니다.
$this->value, 'updated_at' => $this->updated_at, 'metadata' => $this->metadata ]; } } keyBy('key'); return SettingResource::collection($settings); } } class SettingResource extends JsonResource { public $preserveKeys = true; public function toArray($request) { return [ 'value' => $this->formatValue(), 'type' => $this->type, 'last_updated' => $this->updated_at->toDateTimeString(), 'editable' => $this->is_editable ]; } }는 의미있는 키를 유지하고, 특히 데이터를 유지하고 기호합니다. Laravel Resource Collection을 설정함으로써 Laravel Resource Collection은 기본 숫자 인덱스를 사용하는 대신 원래 키를 유지합니다.
{ "data": { "app_name": { "value": "My Application", "type": "string", "last_updated": "2024-03-15 10:30:00", "editable": true }, "max_upload_size": { "value": 10485760, "type": "integer", "last_updated": "2024-03-15 10:30:00", "editable": true } } }
위 내용은 Laravel API 리소스의 컬렉션 키 보존의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!