如何获取最近 1000 条有关系的记录并对其进行分页?我尝试这个,但这会返回所有数据$logbooks = $this->log_book->orderBy('created_at','desc')->skip(10000)->take(1000)->paginate(100);
1 回答
哈士奇WWW
TA贡献1799条经验 获得超6个赞
我唯一能做的就是使用这个宏对集合进行分页,所以将此代码添加到我的 AppServiceProvider 中
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Collection;
Collection::macro('paginate', function ($perPage, $total = null, $page = null, $pageName = 'page') {
$page = $page ?: LengthAwarePaginator::resolveCurrentPage($pageName);
return new LengthAwarePaginator($this->forPage($page, $perPage), $total ?: $this->count(), $perPage, $page, [
'path' => LengthAwarePaginator::resolveCurrentPath(),
'pageName' => $pageName,
]);
});
然后我将对集合进行分页
$logbooks = $this->log_book->orderBy('created_at','desc')->limit(1000)->get();
$logbooks->paginate(100);
- 1 回答
- 0 关注
- 107 浏览
添加回答
举报
0/150
提交
取消