【发布时间】:2019-03-04 17:04:01
【问题描述】:
我有以下简单的索引方法:
public function index()
{
// dd(Poll::paginate(2));
return response()->json(Poll::paginate(2),200);
}
该方法的输出类似于以下 json 对象:
{
"current_page": 1,
"data": [
{
"id": 1,
"title": "I suppose?' said Alice. 'Why, you don't know.",
"created_at": "2018-09-14 16:42:11",
"updated_at": "2018-09-14 16:42:11"
},
{
"id": 2,
"title": "Alice; but she knew that it seemed quite.",
"created_at": "2018-09-14 16:42:11",
"updated_at": "2018-09-14 16:42:11"
}
],
"first_page_url": "http://127.0.0.1:8000/polls?page=1",
"from": 1,
"last_page": 6,
"last_page_url": "http://127.0.0.1:8000/polls?page=6",
"next_page_url": "http://127.0.0.1:8000/polls?page=2",
"path": "http://127.0.0.1:8000/polls",
"per_page": 2,
"prev_page_url": null,
"to": 2,
"total": 11
}
我想在"total: 11"属性之后添加另一个数组属性如:
,
"anotherData" : [
"userId": 1,
"userName": "john10",
"message": "This is a message"
]
我已经尝试了解response()->json() 的工作原理,因此它可以从LengthAwarePaginator 对象中提取一些数据,它是Poll::paginate(2) 从this resource 的输出,但我不能能够理解它如何能够从LengthAwarePaginator 获取一个数组,该数组包含 json 对象中的结果键?!
【问题讨论】:
标签: json laravel laravel-5 response