我遇到了一个问题,在Laravel中对关系进行分页最好的方法是什么?
例如:
$category = $this->blogCategory->whereId($categoryId)->with(['blogPosts'])->first();在这里,我获得了所有必需的数据,但是关系的分页又如何呢?我已经通过first()剥离了类别来检索第一个类别,但是如何对多个博客文章的morphToMany类型的relationship blogPosts进行分页呢?实现这一目标的更好方法是什么?
编辑:
所以我在这个时候想出了这个。
$category = $this->blogCategory->whereId($categoryId)->with(['blogPosts' => function ($query) {
$query->paginate(1);
}])->first();虽然它不能打印出current_page,但它像limit一样工作。
发布于 2021-10-15 20:23:45
使用下面的代码,它应该可以工作,就像我在laravel 5.4中工作一样
$category = $this->blogCategory->whereId($categoryId)->first()->with(['blogPosts'])->paginate(1);https://stackoverflow.com/questions/69589700
复制相似问题