laravel paginate 1000最后一条记录与关系

如何获取最近 1000 条有关系的记录并对其进行分页?

我尝试这个,但这会返回所有数据

$logbooks = $this->log_book->orderBy('created_at','desc')->skip(10000)->take(1000)->paginate(100);


郎朗坤
浏览 108回答 1
1回答

哈士奇WWW

我唯一能做的就是使用这个宏对集合进行分页,所以将此代码添加到我的 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);
打开App,查看更多内容
随时随地看视频慕课网APP