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
Mysql 这个laravel数据库查询有什么问题?_Mysql_Laravel - Fatal编程技术网

Mysql 这个laravel数据库查询有什么问题?

Mysql 这个laravel数据库查询有什么问题?,mysql,laravel,Mysql,Laravel,我有以下疑问: $posts = Post::with( 'comments' => function($c) { $c->orderBy('commentTimestamp', 'desc')->take(6); }) ->where('category_id', '=', $category->id) ->take(20); 我希望它得到20个帖子,并得到每个帖子的最后6条评论。但查询似乎从第一

我有以下疑问:

$posts = Post::with( 'comments' => function($c) {
    $c->orderBy('commentTimestamp', 'desc')->take(6);
})
             ->where('category_id', '=', $category->id)
             ->take(20);

我希望它得到20个帖子,并得到每个帖子的最后6条评论。但查询似乎从第一篇帖子开始只得到6条评论(例如,如果每个帖子都有10条评论,那么查询从第一篇帖子得到6条评论,而从其他帖子没有得到任何评论)。怎么了?

是。。。。。不容易转换为SQL查询。在SQL级别上,基本可行的方法是(1)对每篇文章进行查询或(2)一次查询所有评论,并在userland代码中限制每篇文章。这使得翻译对你来说很有说服力:(1)要么省略with,然后在post上定义一个方法来获取前N(6)条评论,要么(2)无限量地查询所有评论,然后在post上定义一个方法,只返回该帖子中已存储的所有评论中的前N(6)条。谢谢,我认为这就是答案。