【发布时间】:2022-02-11 05:46:08
【问题描述】:
我想知道如何在不使用数据库/模型的 GET 请求中使用给定的 response 对 10 的倍数进行分页。
我已经四处搜索,但我主要看到的是数据库/模型分页的示例,而不是 json 响应分页的示例。
以下是我尝试过的方法(以及许多其他方法,但我已经没有选择余地并碰壁了)但无济于事。
我知道有一种简单的方法可以做到这一点,但我似乎无法理解它。任何反馈将不胜感激:)。
$response = Http::get(env('endpoint'), $queryParams);
$results = $response->json()['results'];
$numResults = $response->json()['num_results'];
$results->paginate(10); // multiples of ten. I know this is the Model way of doing it but I can't find any other way to achieve this.
【问题讨论】:
-
@HashtagForgotName 我根本没有使用模型。这是我仅使用的简单 GET 请求。这是通过使用模型来显示的。
-
Illuminate\Contracts\Support\Jsonable Interface contract and expose the toJson method, so it's very easy to convert your pagination results to JSON.您可以将任何分页结果转换为 json 响应您不需要模型,只需要您的分页结果 -
像这样
DB::table('users')->paginate(15)然后在它后面探测->toJson()所以像这样DB::table('users')->paginate(15)->toJson() -
@HashtagForgotName 非常感谢您的反馈,但我没有使用数据库。这是来自端点的 json 响应。我想对 JSON 响应进行分页。根本没有数据库。
标签: php laravel debugging pagination