Laravel 分页与 ajax 响应
我正在尝试使用 Laravel 创建一个分页系统。在后端,我返回一个如下所示的 JSON 响应:
if(isset($request->myDate)) {
$request->validate([
'myDate' => 'required|date',
]);
$start_date = $request->get('myDate');
$end_date = Carbon::now();
$my_tickets = Ticket::where('admin_id', Auth::user()->id)->whereBetween('updated_at', [$start_date, $end_date])->with(['getAdmin'])->paginate(10);
}
return json_encode($my_tickets);
在前端,我使用 AJAX 请求来显示行。我现在的问题是如何实现分页系统。 Laravel 或第三方组件是否有任何内置功能可以简化此操作?我必须自己构建前端吗?我无法访问 $var->links() 因为它与返回视图的函数不同。
此致
I'm trying to create a pagination system with Laravel. On the backend side I'm returning a JSON response like this:
if(isset($request->myDate)) {
$request->validate([
'myDate' => 'required|date',
]);
$start_date = $request->get('myDate');
$end_date = Carbon::now();
$my_tickets = Ticket::where('admin_id', Auth::user()->id)->whereBetween('updated_at', [$start_date, $end_date])->with(['getAdmin'])->paginate(10);
}
return json_encode($my_tickets);
On the front-end side, I'm using an AJAX request to display the rows. My question now is how to implement the pagination system. Is there any inbuilt function from Laravel or third-party components to simplify this? Do I have to build the front end on my own? I can't access the $var->links() since it's not the same function as returning the view.
Best Regards
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在前端Response中,您可以获取页码、总计数、每页等。如果您想使用自定义ajax分页,则在ajax中使用以下代码。
在控制器中
In the frontend Response, you can get the page number, a total number of counts, per page, etc. if you want to use custom ajax pagination then use the following code in ajax.
In controller