我试图弄清楚为什么在有集合时laravel 5.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": 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']
];
}
}
为什么laravel会更改集合的默认分页,以及如何解决此问题以匹配默认/秒数据集。
在这里构建查询。
/**
* Function: prepareIndexSelect.
*
* @param: $request - Request sent to controller
*
* Description: Returns queried collection.
*/
public static function prepareIndexSelect($request)
{
$model = new self();
$per_page = 50;
$page = 1;
$order_by = $model->getKeyName();
$sort_by = 'asc';
if ($request->has('per_page')) {
$per_page = $request->input('per_page');
}
if ($request->has('sortBy')) {
$order_by = $request->input('sortBy');
}
if ($request->has('sortByAsc')) {
$sort_by = $request->input('sortByAsc');
}
if ($request->has('page')) {
$page = $request->input('page');
}
return $model->when($request->has('fields'), function ($query) use ($request) {
self::prepareSelectColumns($query, $request->input('fields'));
})->when($request->has('includes'), function ($query) use ($request) {
self::prepareIncludedTables($query, $request->input('includes'));
})->whereSearch($request->input('search'))
->orderBy("{$order_by}", "{$sort_by}")
->paginate($per_page, null, null, $page);
}
答案 0 :(得分:1)
查看文档,好像您需要在各自的端点处返回派生的集合。
您的派生资源集合:
<?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']
];
}
}
您的终点:
use App\DeedType;
use App\Http\Resources\DeedTypeCollection;
Route::get('/deed-types', function () {
return new DeedTypeCollection(DeedType::all());
});
您还可以扩展PaginatedResourceResponse
,这应该使您可以覆盖paginationInformation
来以所需的方式格式化响应。
protected function paginationInformation($request)
{
return $this->resource->resource->toArray();
}
然后在您的派生集合中:
public function toResponse($request)
{
return (new MyDerivedPaginatedResourceResponse($this))->toResponse($request)
}