如何在Laravel 5.4中获得评论最多的帖子
我的数据库中有posts和comments表。Comments表将posts id作为外键。现在,如果我想对评论最多的帖子进行排序,如何在laravel中进行排序?这将根据帖子的评论数量对帖子进行排序如何在Laravel 5.4中获得评论最多的帖子,laravel,laravel-5,Laravel,Laravel 5,我的数据库中有posts和comments表。Comments表将posts id作为外键。现在,如果我想对评论最多的帖子进行排序,如何在laravel中进行排序?这将根据帖子的评论数量对帖子进行排序 $posts = Post::withCount('comments') ->orderBy('comments_count', 'desc') ->get(); 这将根据评论数量对帖子进行排序 $posts = Post::withCount('comments')
$posts = Post::withCount('comments')
->orderBy('comments_count', 'desc')
->get();
这将根据评论数量对帖子进行排序
$posts = Post::withCount('comments')
->orderBy('comments_count', 'desc')
->get();
请添加您用于获取用户和评论的代码。请确保这是您试图实现的目标,而不是重复该问题。这两个问题完全不同@rbaskamp请添加您用于获取用户和评论的代码。请确保这是您试图实现的目标,而不是重复该问题。这两个问题完全不同@rbaskam@yasaryousuf没问题。你可以把这个标记为答案。@yasaryouff没问题。你可以把这个标记为答案。