我目前正在尝试此调整,以使每个关系获得N个帖子,例如每个帖子5条评论,而不是总帖子5条评论(问题)。
here提供了该调整,并在SO的许多答案中都提到了。
我的模型非常简单。我已经复制了BaseModel
并为其定义了名称空间。之后,我在关系中加入了以下调整:
public function replies()
{
return $this->hasMany(DismaUserComment::class, 'parent_comment_id', "id")}
->where("visible", 1)
->orderBy("created_at", "ASC");
}
public function repliesLimited()
{
return $this->replies()->latest()->nPerGroup('reply_comment_id', 5);
}
在控制器中,我正在呼叫with('repliesLimited')
。我得到的例外是:
未找到列:1054未知列'(select
app_users_comments
。,@rank:= IF(@group = reply_comment_id,@ rank + 1,1)'在'字段列表'(SQL:选择(select ``app_users_comments```.
,@ rank:= IF(@group = reply_comment_id,@ rank + 1,1)as
rank_807a5773f971e6e6a9db9e1d6ef4f63a,@ group:= reply_comment_id, (select count(*) from
app_users_comments_votes {{1 }} app_users_commentswhere
id.
app_users_comments_votes=
comment_id.
votes_count) as
app_users_comments_votes, (select count(*) from
app_users_commentswhere
id.
app_users_comments_votes=
comment_id.
vote_typeand
upvotes_count= 1) as
app_users_comments_votes, (select count(*) from
app_users_commentswhere
id.
app_users_comments_votes {1}} comment_id { {1}}投票类型=
下降投票计数.
app_users_commentsand
可见= 0) as
app_users_commentsfrom (select
deleted_at.*, @rank := IF(@group = reply_comment_id, @rank+1, 1) as rank_807a5773f971e6e6a9db9e1d6ef4f63a, @group := reply_comment_id as group_807a5773f971e6e6a9db9e1d6ef4f63a from (SELECT @rank:=0, @group:=0) as vars, app_users_comments where
reply_comment_id= 1 and
created_at {{1 }} created_at.
rank_807a5773f971e6e6a9db9e1d6ef4f63ais null order by
app_users_commentsasc,
parent_comment_idasc,
app_users_commentsdesc) as app_users_comments where
deleted_at`为空)”
所需的输出:
获得5条帖子(已完成) 对于每个帖子,应该有2条评论。
基本方法是限制关系,但这将只返回整个评论关系中的2条评论,而不是每个帖子2条评论。