Php Laravel从paginate获取所有项目的数据
我有一本书:Php Laravel从paginate获取所有项目的数据,php,laravel,Php,Laravel,我有一本书: $posts = $posts->paginate(5); 我有一个亲戚: public function category() { return $this->belongsTo(Category::class); } 我需要得到所有计数类别的职位。但是,如果我在页面上只有5篇帖子,怎么办?当我在刀片上执行以下操作时: $posts->pluck('category')->count(); 我只得到1页的文章计数。如何计算所有页面的数量 您可以
$posts = $posts->paginate(5);
我有一个亲戚:
public function category() {
return $this->belongsTo(Category::class);
}
我需要得到所有计数类别的职位。但是,如果我在页面上只有5篇帖子,怎么办?当我在刀片上执行以下操作时:
$posts->pluck('category')->count();
我只得到1页的文章计数。如何计算所有页面的数量 您可以使用计数('category')->分页(5)代码> 看
您可以使用计数('category')->分页(5)代码> 看
我需要做:
$posts->pull('category_count')->count()代码>?用示例更新我的答案,请检查我需要做什么:$posts->pull('category_count')->count()代码>?用示例更新我的答案,请检查
$posts = $posts->withCount('category')->paginate(5);
foreach($posts as $post){
$categoryCountForCurrentPost = $post->category_count;
}