[英]How can I make Laravel eloquent relationship with multiple tables?
I am trying to make 3 tables relationship like this,我正在尝试像这样建立 3 个表关系,
users
id
name
roles
id
name
companies
id
name
company_role_user
user_id
role_id
company_id
Relationships in User.php model User.php model 中的关系
public function companies() {
return $this->belongsToMany(Company::class, 'company_role_user', 'user_id', 'company_id');
}
public function roles() {
return $this->belongsToMany(Role::class, 'company_role_user', 'user_id', 'role_id');
}
public function role() {
// relationship to get role of specific company
}
Relationships in Company.php model Company.php model 中的关系
public function users() {
return $this->belongsToMany(User::class, 'company_role_user', 'company_id', 'user_id');
}
public function roles() {
return $this->belongsToMany(Role::class, 'company_role_user', 'company_id', 'role_id');
}
public function role() {
// relationship to get role of specific user
}
I want to get user role for specific company like this我想获得这样的特定公司的用户角色
User::find(1)->companies[0]->role->name
or或者
Company::find(1)->users[0]->role->name
If you have users如果你有用户
users
– id
– name
and you want them to have many to many relation with companies, you should do:并且您希望他们与公司有多对多的关系,您应该这样做:
companies
– id
– name
user_companies
– user_id
– company_id
And for your roles, (many to many) you can do the same:对于您的角色,(多对多)您可以这样做:
roles
- id
- name
user_roles
- user_id
- role_id
You are trying to make many to many 3 tables, when you should be doing it with 2 tables.您正在尝试制作多对多 3 张桌子,而您应该使用 2 张桌子。
Even if you manage, it would be very complicated and confusing.即使你管理,它也会非常复杂和混乱。
You should consider which tables should be related to roles, companies should have roles, or users should have roles, you dont need them both to have roles你应该考虑哪些表应该与角色相关,公司应该有角色,或者用户应该有角色,你不需要他们都有角色
Your schema looks correct but your model definitions/relations can use a junction/pivot model CompanyRoleUser
which will relate these 3 relations as many-to-one/belongsTo and one-to-many/hasMany from Company/Role/User.您的架构看起来是正确的,但您的 model 定义/关系可以使用连接/枢轴 model
CompanyRoleUser
,它将这 3 个关系关联为多对一/属于和一对多/来自公司/角色/用户的多对多/hasMany。
class Company extends Model
{
public function companyRoleUser()
{
return $this->hasMany(CompanyRoleUser::class, 'id', 'company_id');
}
}
class Role extends Model
{
public function companyRoleUser()
{
return $this->hasMany(CompanyRoleUser::class, 'id', 'role_id');
}
}
class User extends Model
{
public function companyRoleUser()
{
return $this->hasMany(CompanyRoleUser::class, 'id', 'user_id');
}
}
class CompanyRoleUser extends Model
{
public function company()
{
return $this->belongsTo(Discipline::class, 'id', 'company_id');
}
public function role()
{
return $this->belongsTo(Speciality::class, 'id', 'role_id');
}
public function user()
{
return $this->belongsTo(User::class ,'id','user_id');
}
}
Now you can apply different type of filters for your data like现在您可以为您的数据应用不同类型的过滤器,例如
Fetch users for company A id = 1 with admin role id = 2获取公司 A id = 1 的用户,管理员角色 id = 2
User::whereHas('companyRoleUser.company', function ($query) use ($company_id) {
$query->where('id', $company_id);
})->whereHas('companyRoleUser.role', function ($query) use ($role_id) {
$query->where('id', $role_id);
});
Or或者
User::whereHas('companyRoleUser', function ($query) use ($company_id) {
$query->where('company_id', $company_id)
->where('role_id', $role_id);
});
Fetch companies with $user_id
and $role_id
获取具有
$user_id
和$role_id
的公司
Company::whereHas('companyRoleUser', function ($query) use ($user_id) {
$query->where('user_id', $user_id)
->where('role_id', $role_id);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.