I'm currently trying this tweak to get N posts per relation, e.g. 5 comments for each post, instead of 5 comments of the total posts (The issue).
The tweak is provided here and mentioned in many answers on SO.
My model is pretty simple. I've copied the BaseModel and defined the namespace for it. After that, I included the tweak in the relation like this:
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);
}
In the Controller, I'm calling with('repliesLimited'). The exception that I'm getting is:
Column not found: 1054 Unknown column '(select
app_users_comments., @rank := IF(@group = reply_comment_id, @rank+1, 1)' in 'field list' (SQL: select(select ``app_users_comments```., @rank := IF(@group = reply_comment_id, @rank+1, 1)asrank_807a5773f971e6e6a9db9e1d6ef4f63a, @group := reply_comment_id, (select count(*) fromapp_users_comments_voteswhereapp_users_comments.id=app_users_comments_votes.comment_id) asvotes_count, (select count(*) fromapp_users_comments_voteswhereapp_users_comments.id=app_users_comments_votes.comment_idandvote_type= 1) asupvotes_count, (select count(*) fromapp_users_comments_voteswhereapp_users_comments.id=app_users_comments_votes.comment_idandvote_type= 0) asdownvotes_countfrom (selectapp_users_comments.*, @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 wherevisible= 1 andapp_users_comments.deleted_atis null order byreply_comment_idasc,created_atasc,created_atdesc) as app_users_comments whererank_807a5773f971e6e6a9db9e1d6ef4f63a<= 5 andapp_users_comments.parent_comment_idin (1, 12, 13) andapp_users_comments.deleted_at` is null)"
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire