Php Laravel多对多关系返回意外结果
我的多对多关系返回数据有问题。我有3个表来定义这种关系Php Laravel多对多关系返回意外结果,php,laravel-5,many-to-many,Php,Laravel 5,Many To Many,我的多对多关系返回数据有问题。我有3个表来定义这种关系users,institutes和pivot表users\u institutes。myusers\u中的数据在图像中 我的关系由以下代码定义 public function super_user_institutes() { return $this->belongsToMany('App\Institute', 'users_institutes') ->wherePivot('role', 'ins
users
,institutes
和pivot表users\u institutes
。myusers\u
中的数据在图像中
我的关系由以下代码定义
public function super_user_institutes()
{
return $this->belongsToMany('App\Institute', 'users_institutes')
->wherePivot('role', 'inst_superuser')
->orWherePivot('role', 'inst_admin')
->orWherePivot('role', 'inst_staff')
->withPivot('role');
}
现在,对于我试图获取id为2的用户的关系。那么,现在如果我使用
$user->super_user_institutes;
我得到以下行的响应:1、2、3、9、10、11、12(7行)
在这里,我期望得到以下行:1、2、3
我是否期待一个错误的结果?或者我的关系定义错了?请只尝试这么多代码
public function super_user_institutes()
{
return $this->belongsToMany('App\Institute', 'users_institutes');
}
只尝试这么多代码
public function super_user_institutes()
{
return $this->belongsToMany('App\Institute', 'users_institutes');
}
当我写下对我以前所做的事情的否定时,它工作正常。奇怪,但我不明白为什么它现在能用,为什么以前不能用 而不是::
public function super_user_institutes()
{
return $this->belongsToMany('App\Institute', 'users_institutes')
->wherePivot('role', 'inst_superuser')
->orWherePivot('role', 'inst_admin')
->orWherePivot('role', 'inst_staff')
->withPivot('role');
}
我写道:
public function super_user_institutes()
{
return $this->belongsToMany('App\Institute', 'users_institutes')
->wherePivot('role', '<>', 'inst_student')
->withPivot('role');
}
公共功能超级用户协会()
{
返回$this->belongtomany('App\Institute','users\u institutes'))
->wherePivot(‘角色’,‘学院学生’)
->withPivot(“角色”);
}
这将返回我所期望的结果。当我写下对我之前所做的事情的否定时,它工作正常。奇怪,但我不明白为什么它现在能用,为什么以前不能用 而不是::
public function super_user_institutes()
{
return $this->belongsToMany('App\Institute', 'users_institutes')
->wherePivot('role', 'inst_superuser')
->orWherePivot('role', 'inst_admin')
->orWherePivot('role', 'inst_staff')
->withPivot('role');
}
我写道:
public function super_user_institutes()
{
return $this->belongsToMany('App\Institute', 'users_institutes')
->wherePivot('role', '<>', 'inst_student')
->withPivot('role');
}
公共功能超级用户协会()
{
返回$this->belongtomany('App\Institute','users\u institutes'))
->wherePivot(‘角色’,‘学院学生’)
->withPivot(“角色”);
}
这正是我所期待的。没有人知道这一点:(没有人知道这件事?:(