Skip to content

Commit

Permalink
fix: collection resource 格式包含 with 时数据结构错误(fixed #18
Browse files Browse the repository at this point in the history
  • Loading branch information
jiannei committed Mar 19, 2021
1 parent e4891a8 commit c17bf93
Showing 1 changed file with 6 additions and 14 deletions.
20 changes: 6 additions & 14 deletions src/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,9 @@ protected function formatPaginatedResourceResponse($resource, string $message =
$paginationInformation = $this->formatPaginatedData($paginated);

$paginationDataField = Config::get('response.format.paginated_resource.data_field', 'data');
$data = array_merge_recursive([$paginationDataField => $this->parseDataFrom($resource)], $paginationInformation);
$resourceData = array_merge_recursive([$paginationDataField => $resource->resolve(request())], $resource->with(request()), $resource->additional);

$data = array_merge_recursive($resourceData, $paginationInformation);

return tap(
$this->response($this->formatData($data, $message, $code), $code, $headers, $option),
Expand Down Expand Up @@ -351,8 +353,10 @@ function ($item) {
*/
protected function formatResourceResponse($resource, string $message = '', $code = 200, array $headers = [], $option = 0)
{
$resourceData = array_merge_recursive($resource->resolve(request()), $resource->with(request()), $resource->additional);

return tap(
$this->response($this->formatData($this->parseDataFrom($resource), $message, $code), $code, $headers, $option),
$this->response($this->formatData($resourceData, $message, $code), $code, $headers, $option),
function ($response) use ($resource) {
$response->original = $resource->resource;

Expand All @@ -361,18 +365,6 @@ function ($response) use ($resource) {
);
}

/**
* Parse data from JsonResource.
*
* @param JsonResource $data
*
* @return array
*/
protected function parseDataFrom(JsonResource $data): array
{
return array_merge_recursive($data->resolve(request()), $data->with(request()), $data->additional);
}

/**
* Return a new JSON response from the application.
*
Expand Down

0 comments on commit c17bf93

Please sign in to comment.