![](/img/trans.png)
[英]Laravel get All with Relationships and Paginate relationship data
[英]Laravel get data from all items from paginate
我有一个念头:
$posts = $posts->paginate(5);
我有关系:
public function category() {
return $this->belongsTo(Category::class);
}
我需要获取帖子的所有计数类别。 但是,如果在页面上我只有5个帖子,该怎么办? 当我在刀片上进行操作时:
$posts->pluck('category')->count();
我只计数1页的帖子。 如何获得所有页面的计数?
您可以执行$posts->withCount('category')->paginate(5);
参阅文件
$posts = $posts->withCount('category')->paginate(5);
foreach($posts as $post){
$categoryCountForCurrentPost = $post->category_count;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.