我有一个论坛和论坛响应模型,其中包含以下数据库表:
forum.id
forum_response.id
forum_response.forum_id
forum_response.user_id
forum_response.text
论坛模型关系是:
public function responses()
{
return $this->belongsToMany(ForumResponse::class, 'forum__responses');
}
和论坛回应关系:
public function Forum()
{
return $this->belongsTo(Forum::class);
}
我想获得特定论坛的唯一回复数量,按user_id分组。我尝试过以下return $this->hasMany(ForumResponse::class)->groupBy('user_id')->count();
,但这会返回比我预期更高的值。
答案 0 :(得分:0)
尽管如此,我觉得你的结构有问题。但就目前而言,您的人际关系中存在错误。只需将belongsToMany更改为hasMany
即可更改此
public function responses()
{
return $this->belongsToMany(ForumResponse::class, 'forum__responses');
}
到这个
public function responses()
{
return $this->hasMany(ForumResponse::class, 'forum__responses');
}