Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Laravel 5.4中获得评论最多的帖子_Laravel_Laravel 5 - Fatal编程技术网

如何在Laravel 5.4中获得评论最多的帖子

如何在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和comments表。Comments表将posts id作为外键。现在,如果我想对评论最多的帖子进行排序,如何在laravel中进行排序?

这将根据帖子的评论数量对帖子进行排序

$posts = Post::withCount('comments')
    ->orderBy('comments_count', 'desc')
    ->get();

这将根据评论数量对帖子进行排序

$posts = Post::withCount('comments')
    ->orderBy('comments_count', 'desc')
    ->get();

请添加您用于获取用户和评论的代码。请确保这是您试图实现的目标,而不是重复该问题。这两个问题完全不同@rbaskamp请添加您用于获取用户和评论的代码。请确保这是您试图实现的目标,而不是重复该问题。这两个问题完全不同@rbaskam@yasaryousuf没问题。你可以把这个标记为答案。@yasaryouff没问题。你可以把这个标记为答案。