gpt4 book ai didi

Laravel 分页获取下一页

转载 作者:行者123 更新时间:2023-12-04 14:20:41 24 4
gpt4 key购买 nike

我有一个 api,它将获取按 10 个项目分页的数据。这是我的逻辑:

public function getGamesResult(Request $request)
{
// Validations
$rules = [
'from'=>'required|string',
'to'=> 'required|string'
];

$validator = Validator::make($request->all(), $rules);

if ($validator->fails()) {

// Validation failed
return response()->json([
'success' => false,
'message' => $validator->messages(),
]);
}

$params = $request->all();

$games = Game::whereBetween("game_date", [$params["from"], $params["to"]])->paginate(10);
$result = [];

foreach ($games as $game) {

$win_data = [];
$winners = $game->result()->get();

// Get Winning Number Records
foreach ($winners as $win)
{
$winning_numbers_arr = explode(',', $win->winning_numbers);


$win_data[] = [
'game_id' => $win->game_id,
'created_dt' => $win->created_dt
];

}

$result[] = [
'game_data' => [
'game_id' => $game->id,
'game_date' => $game->game_date
],
'winning_number' => $win_data
];
}

return response()->json([
'success' => true,
'data' => $result,
'total'=> $games->total(),
'count'=> $games->count(),
'hasMorePages'=> $games->hasMorePages(),
'currentPage'=> $games->currentPage()
]);
}

我的问题是 laravel 如何知道我何时要获取下 10 条记录?还是我做错了?如果总共有 3000 条记录,我将如何使用相同的逻辑获取下一页?

最佳答案

Laravel 的分页器包含自动获取请求的下一页的所有逻辑

https://laravel.com/docs/5.8/pagination#paginating-query-builder-results

如果您在 url 末尾添加 ?page=2,它将获取第二页。如果未传递任何参数,则假定为第 1 页。

关于Laravel 分页获取下一页,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55391876/

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