假设我有一个这样的分页
return App\Post::paginate(1);加载后,有人在数据库中为帖子创建了一个新条目。如何确保分页的“第二页”不会再次返回第一页?
在使用分页之前,我是否总是需要在每次请求时都发送一个时间戳来进行额外的查询?喜欢
App\Post::where('created_at', '<', $request->input('max_created_at'))->paginate(1);发布于 2017-01-24 16:36:40
您可以使用orderBy。你可以这样做:
// Get the last post (most recent)
return App\Post::orderBy('created_at', 'DESC')->paginate(1);或者:
// Same as above
return App\Post::orderBy('created_at', 'DESC')->first();orderBy表示在查询中对所有结果进行排序。希望这能有所帮助。
发布于 2017-01-24 17:12:10
您不需要传递任何时间戳值来验证分页。在控制器中,在视图文件中,您需要传递此查询的结果。
$result = App\Post::orderBy('created_at', 'desc')->paginate(1);
return view('pagination',compact('result'));在视图文件中,您需要下面的代码行来根据Laravel语法自动进行分页工作。
{{ $result->links() }}访问https://laravel.com/docs/5.2/pagination/:laravel分页是如何工作的。
https://stackoverflow.com/questions/41823187
复制相似问题