首页 > 解决方案 > Laravel 枢轴关系没有指定其中之一

问题描述

我有一个表来保存应用程序上可用的所有反应(反应)和 N 个表以与数据透视表的多对多关系与此相关。

前任: 风险管理

// Post Model
public function reactions()
{
    return $this->belongsToMany(Reaction::class, 'reactions_posts')
        ->with('reactors')->groupBy('reaction_id');
}

// Reactions Model
public function post()
{
    return $this->hasMany(Post::class, 'reactions_posts');
}

public function reactors(){
    return $this->hasMany(User::class, 'reactions_posts');
}

一旦 N 个模型可以Reactions使用枢轴与模型相关联,我正在寻找一种更好的方法来关联 Post x Reaction、Event x Reaction 以及将使用相同原理关联的所有必需模型,因为这种方式会产生 Post 的一些依赖性, 事件和所有的反应模型。

标签: phplaraveleloquenteloquent-relationship

解决方案


推荐阅读