我得到了评论的分页。我需要检查,如果我有评论的ID,然后打开评论所在的页面。这是我的代码:
public function show_comments(Request $request) {
$cmnt_id = $request->input('cmnt_id');
if ( isset( $cmnt_id ) ){
// I need to get all comments paginated but start from the page that $cmnt_id is in it
} else {
// starts from page 1
$comments = Cmnt_tb::paginate(10);
}
return View('show_cmnts', compact('comments'));
}
我该怎么做?任何想法?
我想我必须创建一个新网址并重定向到它。
答案 0 :(得分:1)
也许有更好的解决方案,但我想这样的事情对你有用:
public function show_comments(Request $request) {
$cmnt_id = $request->input('cmnt_id');
$perPage = 10;
if (isset($cmnt_id)){
// Get row number.
$rowNumber = Cmnt_tb::where('id', '<=', $cmnt_id)->count();
// Get page.
$page = ceil($rowNumber / $perPage);
return redirect('show-comments-url?page='.$page);
} else {
$comments = Cmnt_tb::paginate($perPage);
return view('show_cmnts', compact('comments'));
}
}