【发布时间】:2020-05-17 18:53:42
【问题描述】:
我有以 JSON 格式返回的资源数据。当我尝试使用分页获取数据时,它不包含元数据。
基于documentation 我的数据应该包含在元数据中,例如:
"meta":{
"current_page": 1,
"from": 1,
"last_page": 1,
"path": "http://example.com/pagination",
"per_page": 15,
"to": 10,
"total": 10
}
但我的数据返回如下:
代码
controller
public function index()
{
$products = ProductFrontResource::collection(Product::orderby('id', 'desc')->with(['photos', 'seo', 'tags', 'variations', 'variations.children', 'options', 'options.children', 'categories'])->where('active', 'yes')->paginate(8));
return response()->json([
'data' => $products,
'message' => 'Products retrieved successfully.',
]);
}
有什么想法吗?
【问题讨论】:
-
我认为你也必须使用分页功能:ProductFrontResource::collection (another query->paginate(8))->paginate(8);
-
@MubasharIqbal 抱歉没有明白你的意思!请给个样品好吗?