php - laravel paginate 1000 last Record with relationship
问题描述
如何获取最后 1000 条有关系的记录并对其进行分页?
我试试这个,但这会返回所有数据
$logbooks = $this->log_book->orderBy('created_at','desc')->skip(10000)->take(1000)->paginate(100);
解决方案
只有我能做的是使用这个宏在集合上进行分页,所以将此代码添加到我的 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);
推荐阅读
- css - HTML5 + CSS:DIV左右浮动
- c# - 多对多自我参考
- tomcat - Tomcat 问题:我尝试部署 WAR 文件,但收到此消息:源服务器未找到目标资源的当前表示
- python - Python在读取JSON文件时抛出错误
- swift - 从其他视图控制器执行功能
- c# - 如何使用数据网格视图列值按升序排序?
- javascript - 如何在 JS 中映射对象数组并为每个对象获取网络数据?
- r - 如何使用 r 中的 if-else 循环替换时间序列数据的缺失值
- python - 如何从使用 plot_date 生成的图形中删除点?
- c++ - 使用 OpenSSL 作为静态库在 Visual Studio 中编译项目