php - 为什么 laravel 分页只适用于 reture 而不是自定义响应处理程序
问题描述
如果你看到这段代码:
use ResponseHandler;
public function search(SoundFilter $soundFilter) {
$sounds = Sound::query();
$sounds->filter($soundFilter);
// dd($sounds->toSql());
$sounds = $sounds->paginate(1);
$this->setResponseStatus(1);
$this->setResponseStatusCode(200);
$this->setResponseData([
'sounds' => new SoundsCollection($sounds),
]);
$this->ajaxResponse();
return $this->response();
}
我想要soundsCollection($sounds) 返回分页结果,但它只会发送数据
{
"status": 1,
"messages": [],
"statusCode": 200,
"data": {
"sounds": [
{
"id": 1,
"name": "منی که عشق با روضه فهمیدم",
"slug": "",
"type": 2,
"description": "<p dir=\"rtl\" style=\"text-align:center\"><strong>متن شعر<\/strong><\/p>",
"likes": 0,
"downloads": 0,
"views": 0,
"created_at": "2019-08-02 20:09:45",
"updated_at": "2019-08-03 18:45:49",
"sort_order": 0,
"status": 1,
}
]
}
}
但是当我执行此代码时:
$sounds = Sound::query();
$sounds->filter($soundFilter);
// dd($sounds->toSql());
$sounds = $sounds->paginate(1);
$this->setResponseStatus(1);
$this->setResponseStatusCode(200);
// $this->setResponseData([
//// 'sounds' => new SoundsCollection($sounds),
//// ]);
$this->ajaxResponse();
return new SoundsCollection($sounds);
我会得到分页结果
{
"data": [
{
"id": 1,
"name": "منی که عشق با روضه فهمیدم",
"slug": "",
"type": 2,
"description": "<p dir=\"rtl\" style=\"text-align:center\"><strong>متن شعر<\/strong><\/p>",
"likes": 0,
"downloads": 0,
"views": 0,
"created_at": "2019-08-02 20:09:45",
"updated_at": "2019-08-03 18:45:49",
"sort_order": 0,
"status": 1,
}
],
"links": {
"first": "http:\/\/madahi.test\/api\/v1.0\/search?page=1",
"last": "http:\/\/madahi.test\/api\/v1.0\/search?page=3",
"prev": null,
"next": "http:\/\/madahi.test\/api\/v1.0\/search?page=2"
},
"meta": {
"current_page": 1,
"from": 1,
"last_page": 3,
"path": "http:\/\/madahi.test\/api\/v1.0\/search",
"per_page": 1,
"to": 1,
"total": 3
}
}
我应该调用哪个函数或者我应该做什么来获得相同的结果但是使用我的自定义响应处理程序如何强制收集返回分页结果我希望我有链接,第一个结果中的元
解决方案
如果有同样的问题,我找到了它并分享给其他人:因为我使用单独的 ResponseHandler 并且它会重新发送一个 JsonResponse 对象,它是我发送给 responseHandler 的数据
// trait : ResponseHandler.php
new JsonResponse($json, $json['statusCode'], $this->headers);
当我调用此代码时,它将重新调整另一个 JsonResponse
'sounds' => new SoundsCollection($sounds),
// Or
'sounds' => (new SoundsCollection($sounds))->toResponse($request)
所以上面的 JsonResponse 将忽略分页数据,解决方案是:
'sounds' => (new SoundsCollection($sounds))->toResponse($request)->getData()
它会起作用
{
"status": 1,
"messages": [],
"statusCode": 200,
"data": {
"sounds": {
"data": [
{
"id": 1,
"name": "منی که عشق با روضه فهمیدم",
"slug": "",
"type": 2,
"description": "<p dir=\"rtl\" style=\"text-align:center\"><strong>متن شعر<\/strong><\/p>",
"likes": 0,
"downloads": 0,
"views": 0,
"created_at": "2019-08-02 20:09:45",
"updated_at": "2019-08-03 18:45:49",
"sort_order": 0,
"status": 1,
}
],
"links": {
"first": "http:\/\/madahi.test\/api\/v1.0\/search?page=1",
"last": "http:\/\/madahi.test\/api\/v1.0\/search?page=3",
"prev": null,
"next": "http:\/\/madahi.test\/api\/v1.0\/search?page=2"
},
"meta": {
"current_page": 1,
"from": 1,
"last_page": 3,
"path": "http:\/\/madahi.test\/api\/v1.0\/search",
"per_page": 1,
"to": 1,
"total": 3
}
}
}
}
推荐阅读
- azure-devops - 发布后将文件从 azure 文件存储复制到 azure 网站
- c# - 如何从列表属性对象分配给十进制列表和字符串列表并比较十进制
- javascript - Raycaster / Touch 没有在带有 THREE.DragControls 的移动设备上注册
- sql-server - 尽管使用了数据转换和准确的目标数据类型,但 SSIS 数据转换错误
- graph - 多个 Web 应用程序注册在 AAD 中不起作用
- node.js - 如何处理 gRPC 错误导致 Node 应用程序崩溃
- ruby - Ruby:对大量记录进行分页和排序
- python - 如果 Python max() 包含两个列表,它会做什么?
- python - 如何修复pygame闪烁
- python - Django 接受 QueryParams(必须是可调用错误)