Laravel 拉维收银申报表
我试图弄明白为什么Laravel5.6在有一个集合时,它会打印出分页的json响应,而不是在分页列表上使用带有json的正常返回响应。所以我现在收藏了这个Laravel 拉维收银申报表,laravel,rest,laravel-5.6,Laravel,Rest,Laravel 5.6,我试图弄明白为什么Laravel5.6在有一个集合时,它会打印出分页的json响应,而不是在分页列表上使用带有json的正常返回响应。所以我现在收藏了这个 { "data": [], "links": { "first": "http://local/api/v3/x?=1", "last": "http://local/api/v3/x?=1", "prev": null, "next": null }, "meta": { "current_page"
{
"data": [],
"links": {
"first": "http://local/api/v3/x?=1",
"last": "http://local/api/v3/x?=1",
"prev": null,
"next": null
},
"meta": {
"current_page": 1,
"from": 1,
"last_page": 1,
"path": "http://local/api/v3/x",
"per_page": "5",
"to": 3,
"total": 3
}
}
我希望它看起来像是:
{
"current_page": 1,
"data": [],
"first_page_url": "http://local/api/v3/x?=1",
"from": null,
"last_page": 1,
"last_page_url": "http://local/api/v3/x?=1",
"next_page_url": null,
"path": "http://local/api/v3/x",
"per_page": 50,
"prev_page_url": null,
"to": null,
"total": 0
}
我曾尝试在集合中使用以下代码,但它不起作用,我找到了一些函数来获取我想要的数据,但我仍然缺少大部分数据
<?php
namespace App\Http\Resources\V3;
use Illuminate\Http\Resources\Json\ResourceCollection;
class DeedTypeCollection extends ResourceCollection
{
public $collects = 'App\Http\Resources\V3\x';
/**
* Transform the resource collection into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
return [
'current_page' => $this->currentPage(),
'data' => $this->collection,
'first_page_url' => $this->links['first'],
'from' => $this->meta['from'],
'last_page' => $this->meta['last_page'],
'last_page_url' => $this->links['last'],
'next_page_url' => $this->links['next'],
'path' => $this->meta['path'],
'per_page' => $this->meta['per_page'],
'prev_page_url' => $this->links['prev'],
'to' => $this->meta['to'],
'total' => $this->meta['total']
];
}
}
查看文档时,似乎需要在相应的端点返回派生集合 您的派生资源集合:
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\ResourceCollection;
class DeedTypeCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
{
return [
'current_page' => $this->currentPage(),
'data' => $this->collection,
'first_page_url' => $this->links['first'],
'from' => $this->meta['from'],
'last_page' => $this->meta['last_page'],
'last_page_url' => $this->links['last'],
'next_page_url' => $this->links['next'],
'path' => $this->meta['path'],
'per_page' => $this->meta['per_page'],
'prev_page_url' => $this->links['prev'],
'to' => $this->meta['to'],
'total' => $this->meta['total']
];
}
}
public function toResponse($request)
{
return (new MyDerivedPaginatedResourceResponse($this))->toResponse($request)
}
您还可以扩展PaginatedResourceResponse
这将允许您覆盖paginationInformation
以按照需要的方式格式化响应
protected function paginationInformation($request)
{
return $this->resource->resource->toArray();
}
然后在派生集合中:
<?php
namespace App\Http\Resources;
use Illuminate\Http\Resources\Json\ResourceCollection;
class DeedTypeCollection extends ResourceCollection
{
/**
* Transform the resource collection into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
public function toArray($request)
{
return [
'current_page' => $this->currentPage(),
'data' => $this->collection,
'first_page_url' => $this->links['first'],
'from' => $this->meta['from'],
'last_page' => $this->meta['last_page'],
'last_page_url' => $this->links['last'],
'next_page_url' => $this->links['next'],
'path' => $this->meta['path'],
'per_page' => $this->meta['per_page'],
'prev_page_url' => $this->links['prev'],
'to' => $this->meta['to'],
'total' => $this->meta['total']
];
}
}
public function toResponse($request)
{
return (new MyDerivedPaginatedResourceResponse($this))->toResponse($request)
}
你能把代码贴在分页发生的地方吗。Laravel没有用于集合的分页方法。根据您的请求进行更新。基本上,正常的分页显示了我想要的方式,但在集合中,它改变了输出的格式,这将通过我的api实现。我相信返回的内容试图遵循json api规范。看看雄辩的资源:。规范:Adam,我开始认为你是对的,我已经能够删除链接和元数据,但我无法提取这些信息,并用不同的格式格式化我自己的收藏申报单。路线申报单和格式不是我面临的问题。我遇到的问题是特定于“数据”“链接”“元”的收集标准返回。我想要所有这些信息,但不是以那种方式的格式。我想要这些信息之外的链接和元信息attributes@AlexanderBeyer在挖掘源代码之后,您可能必须重写
以响应
方法。是的,你必须重写toResponse方法来完成我想要的,我不想为api做这件事。我将把它记为对拉威尔标准的一个限制,我们正在尽可能地保持开箱即用的拉威尔。