![](/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.