gpt4 book ai didi

php - 使用 Resource 显示数据后分页不显示

转载 作者:行者123 更新时间:2023-12-05 07:20:15 26 4
gpt4 key购买 nike

我在使用资源显示数据后遇到分页问题。在此之前,我使用这段代码来显示数据,输出也显示了分页。

输出 enter image description here

Controller

$All = Customers::with('order')->paginate(10);

return response()->json([
'code' => 0,
'success' => true,
'data' => $All,
], 200);

但是在我使用 Resources 显示数据之后,分页就消失了。

$All = Customers::with('order')->paginate(10);

return response()->json([
'code' => 0,
'success' => true,
'data' => DataResource::collection($All),
], 200);

数据资源

public function toArray($request)
{
//return parent::toArray($request);
return [
'name' => $this->name,
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'order' => Resources::collection($this->order)
];
}

最佳答案

看来您正在使用 Eloquent: API Resources , 虽然不确定使用哪个版本的 Laravel 因为较新的版本显示了非常不同的分页。

当我显示第一个片段的 dump() 时,分页器(也称为 LengthAwarePaginator 对象)位于响应的根目录中:

LengthAwarePaginator {#406 ▼
#total: 3
#lastPage: 1
#items: Collection {#401 ▶}
#perPage: 10
#currentPage: 1
#path: "http://laravelsoplayground"
#query: []
#fragment: null
#pageName: "page"
+onEachSide: 3
#options: array:2 [▶]
}

应用资源后,分页器仍然存在,但已移至 resource 属性。

AnonymousResourceCollection {#331 ▼
+collects: "App\Http\Resources\DataResource"
+collection: Collection {#214 ▶}
+resource: LengthAwarePaginator {#406 ▶}
+with: []
+additional: []
}

关于php - 使用 Resource 显示数据后分页不显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57550097/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com