我想接收一个数据数组,像这样:
"data": [
"6": {
"title": "..",
"photo": ..
}
]
但是我正在接收一个对象:
"data": {
"6": {
"title": "..",
"photo": ".."
}
}
另一个有趣的事实是,对于第一页,我正在收到需要的内容:
"data": [
"1": {
"title": "..",
"photo": ..
},
.
.
.
"6": {
"title": "..",
"photo": ..
}
]
这是我在控制器中的功能:
$lists = [];
$cooks = cook::all();
foreach ($cooks as $cook) {
$list = [
'title' => $cook->title,
'photo' => $cook->photos->path,
'recipe' => $cook->recipe->description
];
$lists[] = $list;
}
$collection = $this->paginate($lists, $perPage = 6, $page = null, $options = []);
return new mainCollection($collection);
我具有以下分页功能:
public function paginate($items, $perPage = 15, $page = null, $options = [])
{
$page = $page ?: (Paginator::resolveCurrentPage() ?: 1);
$items = $items instanceof Collection ? $items : Collection::make($items);
return new LengthAwarePaginator($items->forPage($page, $perPage), $items->count(), $perPage, $page, $options);
}
以下是我的resourceColection(mainColection):
public function toArray($request)
{
return [
'data' => $this->collection,
'meta' => ['pagination' => $this->pagination]
];
}
答案 0 :(得分:1)
由于正在使用集合,因此可以使用laravel映射。
$array = $this->collection->map(function ($item, $key) {
return [
'title': "tort",
...
]
});
并将其编码为json
public function toArray($request)
{
return [
'data' => json_encode($array),
'meta' => ['pagination' => $this->pagination]
];
}
您还可以在此处阅读文档:https://laravel.com/docs/5.8/collections