[英]laravel many-to-many relationship return non related data
I have a relationship in my applications that is basically, 我的应用程序中基本上有一个关系,
Many Organisations can have many Users, and many Users can have many Organisations. 许多组织可以有许多用户,许多用户可以有许多组织。
So a many-to-many relationship, the organisation model relationship looks like this, 因此,多对多关系,组织模型关系看起来像这样,
public function users() {
return $this->belongsToMany('User')
->where('admin', '>', 0)
->orWhere('basic', '>', 0)
->withPivot([
'start_date' => 'start_date as start_date',
'admin' => 'admin as admin',
'manager' => 'manager as manager',
'finance' => 'finance as finance',
'basic' => 'basic as basic',
'notifications' => 'notifications as notify'
])
->withTimestamps();
}
and my user model relationship to organisations looks like this, 我与组织的用户模型关系如下所示,
public function organisations()
{
return $this->belongsToMany('Organisation');
}
I am having a huge problem with this relationship however, when I access this through project ( a project has one organisation, that has many users), I get a full list of users rather than just the users of the organisation for that project. 但是,我与这种关系存在很大的问题,当我通过项目访问该项目时(一个项目有一个组织,有很多用户),我会获得完整的用户列表,而不仅仅是该项目的组织用户。
Why would this be? 为什么会这样呢? I think it is to do with my where clauses in the Users() function in my organisation model? 我认为这与我的组织模型中Users()函数中的where子句有关吗?
I don't see the reason you use all this code! 我看不到您使用所有这些代码的原因! I would say, Just make it simple: 我会说,简单一点:
Organisation class 组织类
public function users() {
return $this->belongsToMany('App\User');
}
User class 用户类别
public function organisations() {
return $this->hasMany('App\Organisation');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.