我想使用skip
和take
,但分页不起作用。为什么不起作用?
代码:
$posts = DB::table('posts')
->orderBy('id','desc')
->skip(9)
->take(3)
->paginate(3);
if ($request->ajax()) {
return ['posts' => view('blog.ajax.index')->with(compact('posts'))->render(),
'next_page' => $posts->nextPageUrl()
];
}
我绝对需要使用分页
答案 0 :(得分:1)
您不能同时使用paginate和skip。所以你的替代选择将是这样的
$take = 100;
$skip = 9;
$currentPage = Request::get('page', 1);
$poster = DB::table('posts')
->take(100)
->skip($skip + (($currentPage - 1) * $take))
->orderBy('id','desc');
分页尝试
@if (Request::has('page') && Request::get('page') > 1)
<a href="{{ route('some.route', ['page' => Request::get('page') - 1]) }}">Previous page</a>
@endif
@if (Request::has('page'))
<a href="{{ route('some.route', ['page' => Request::get('page') + 1]) }}">Next page</a>
@else
<a href="{{ route('some.route', ['page' =>2]) }}">Next page</a>
@endif