Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.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
Php 限制即时加载的注释_Php_Sql_Laravel_Laravel 4_Eloquent - Fatal编程技术网

Php 限制即时加载的注释

Php 限制即时加载的注释,php,sql,laravel,laravel-4,eloquent,Php,Sql,Laravel,Laravel 4,Eloquent,我想在所有新闻中添加评论。现在,我正在检索包含所有评论的所有新闻。但是,我只希望每个新闻项目显示2条评论 $news=news::with('user')->with('comments.user')->orderBy('created_at','desc')->get() 如何在每个新闻项目中只获得2条评论?我不确定这是否可行 您可以这样做: News::with(array('comments.user' => function($q) { $q->take(2); }));

我想在所有新闻中添加评论。现在,我正在检索包含所有评论的所有新闻。但是,我只希望每个新闻项目显示2条评论

$news=news::with('user')->with('comments.user')->orderBy('created_at','desc')->get()


如何在每个新闻项目中只获得2条评论?

我不确定这是否可行

您可以这样做:

News::with(array('comments.user' => function($q) { $q->take(2); }));
但它将从整个集合中获取两条注释(generatedquery看起来像是用户的
SELECT*,其中id位于(…)LIMIT 2


我也有同样的问题。我用了一种肮脏的方式——迭代新闻记录,获取两条评论并缓存整个内容。

使用这种方法,整个页面只会获取两条评论:-p我想我必须计算评论,只选择最新的评论two@radmen你能简单地告诉我你是怎么用那种肮脏的方式做事的吗?