Eloquent 計算關聯數量

假設我們有這兩個 Model

1
2
3
4
5
6
7
8
9
10
11
12
13
14

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
public function comments()
{
return $this->hasMany(Comment::class);
}
}

class Comment extends Model
{
}

我們希望能計算出每篇文章的留言數,倒底應該怎麼寫比較好呢?其實 Eloquent 已經提供一個很簡單的 method withCount

1
2
3
Post::withCount('comments')->get()->each(function($post) {
echo $post->comments_count;
});

但如果 withCount 有 where condition 時又該如何處理呢?

1
2
3
4
5
Post::withCount(['comments' => function($query) {
$query->where('name', 'recca0120');
}])->get()->each(function($post) {
echo $post->comments_count;
});

這樣就完成了