为什么Pagination laravel会忽略请求 - 跳过,抓住

时间:2017-07-20 06:01:02

标签: laravel pagination skip take

我想使用skiptake,但分页不起作用。为什么不起作用?

代码:

    $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()
        ];
    }

我绝对需要使用分页

1 个答案:

答案 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