【发布时间】:2018-06-14 03:39:47
【问题描述】:
我一直在使用 laravel api 资源。默认情况下,laravel 提供如下所示的链接和元数据。
"links": {
"first": "https://demo.test/api/v2/taxes?page=1",
"last": "https://demo.test/api/v2/taxes?page=4",
"prev": null,
"next": "https://demo.test/api/v2/taxes?page=2"
},
"meta": {
"current_page": 1,
"from": 1,
"last_page": 4,
"path": "https://demo.test/api/v2/taxes",
"per_page": 2,
"to": 2,
"total": 8
}
但我不想要这个,我想要类似的东西
"pagination": {
"total": 8,
"count": 8,
"per_page": 25,
"current_page": 1,
"total_pages": 1
}
我能够获得此信息,但如果我这样做return TaxResource::collection($taxes);,我将无法获得此信息。即使我有自定义收集方法
public static function collection($resource)
{
$resource->pagination = [
'total' => $resource->total(),
'count' => $resource->count(),
'per_page' => $resource->perPage(),
'current_page' => $resource->currentPage(),
'total_pages' => $resource->lastPage()
];
return parent::collection($resource);
}
它没有给我想要的东西。但是,如果我通过(TaxResource::collection($taxes))->pagination; 进行引用,我就能得到它。但我希望它在我做@987654327@时被退回
【问题讨论】:
-
如果每个人都想知道分页实例的可用方法/属性是什么,请查看:laravel.com/api/8.x/Illuminate/Pagination/…
标签: laravel-5.5