如何使用 Eloquent 统计一个帖子的评论数并按评论数对帖子进行排序?
我的代码大致如下:
class Post extends Model
{
protected $table = 'posts';
public function comments()
{
return $this->hasMany('App\Comment');
}
}
我需要以优雅的方式检索评论数排序的帖子集合,因此我希望不要使用像DB::select(select count(comment.post_id), post.id from posts left join comments on posts.id = comments.post_id group by post.id order by count(post.id))
这样的东西;