3

我想使用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()
        ];
    }

我绝对需要使用分页

4

1 回答 1

1

您不能同时使用分页和跳过。所以你的替代选择是这样的

$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
于 2017-07-20T06:17:16.707 回答