Laravel - 有效地限制每个儿童用品 [英] Laravel - Limit each child item efficiently

查看:103
本文介绍了Laravel - 有效地限制每个儿童用品的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个:

$commentReplies = Comment::whereIn('comment_parent_id', $CommentsIDs)
                                  ->take(2)->get();

其中 $ CommentsIDs 是一个3父注释ids(1,2,3)。

Where $CommentsIDs is an array of 3 parent comment ids (1,2,3).

我正在为每个$ commentsID(如果存在)检索2个回复。
所以总共6个回复(每个评论2个)应该返回查询,如果回复存在,没有更多。然而,随着(2)在那里,它将答复限制为2,我们只收到2个回复的其中一个意见。
如何设置以最有效的方式获取每个注释ID的2条回复,以及如何在视图中以正确的嵌套方式呈现?

I am trying to retrieve 2 replies for each of the $commentsIDs if they exist. So a total of 6 replies (2 for each comment) should come back with the query, if the replies exist, nothing more. However, with take(2) in there, it limits the replies to 2, and we only get 2 replies for one of the comments. How can it be setup to get 2 replies for each of the comment IDs in the most efficient way, and how can they get rendered in the view with the correct nesting?

如下:

Comment 1
--Comment 1 Reply 1 (load this)
--Comment 1 Reply 2 (load this)
--Comment 1 Reply 3 (don't load this)
--Comment 1 Reply 4 (don't load this)
Comment 2
--Comment 2 Reply 1 (load this)
--Comment 2 Reply 2 (load this)
--Comment 2 Reply 3 (don't load this)
Comment 3
(no replies, don't load anything)

更新:

这是评论模型:

Update:
Here is the Comment Model:

class Comment extends BaseModel {

 public function latestTwoComments()
 {
     return $this->hasMany('Comment','comment_parent_id')->latest()->nPerGroup('comment_parent_id', 2);
 }

}

查询:

$comments = Comment::with('latestTwoComments')->get();
dd(DB::getQueryLog());

// Result:
'query' => string 'select * from (select `comments`.*, @rank := IF(@group = comment_parent_id, @rank+1, 1) as rank_575b053fb57f8fab5bc86dd324b39b91, @group := comment_parent_id as group_575b053fb57f8fab5bc86dd324b39b91 from (SELECT @rank:=0, @group:=0) as vars, comments where `comments`.`deleted_at` is null order by `comment_parent_id` asc, `created_at` desc) as comments where `comments`.`deleted_at` is null and `rank_575b053fb57f8fab5bc86dd324b39b91` <= ? and `comments`.`comment_parent_id` in (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?'... (length=603)


推荐答案

您不能使用限制 / skip 当加载时,会限制整个相关的结果。

You can't use limit/skip when eager loading, for it will limit whole related result.

我想你使用 MySQL ,所以这里是你需要的: http://softonsofa.com/tweaking-eloquent-relations-how-to-get-n-related-models-per-parent/

I suppose you use MySQL, so here is what you need: http://softonsofa.com/tweaking-eloquent-relations-how-to-get-n-related-models-per-parent/

这是很长的粘贴在这里,所以只是为了得到这样的想法:你需要MySQL变量来为您的父母提取n的工作,如:

It's way to long to paste here, so just to get the idea: You need MySQL variables to do the job of fetching n per parent for you, like:

public function latestTwoComments()
{
  return $this->hasMany('Comment', 'comment_parent_id')->latest()->nPerGroup('comment_parent_id', 2);
}

//then
$comments = Comment::with('latestTwoComments')->get();
// now all the comments will have at most 2 related child-comments

注意:这意味着 hasMany 关系和 MySQL

Note: it's meant for hasMany relation and MySQL

这篇关于Laravel - 有效地限制每个儿童用品的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆