Laravel 黑科技:Eloquent 裡的 withCount

jake666發表於2019-05-28
$posts = App\Post::withCount([
    'comments',
    'comments as pending_comments_count' => function ($query) {
        $query->where('approved', false);
    }
])->get();

echo $posts[0]->comments_count;

echo $posts[0]->pending_comments_count;

相關文章