【问题标题】:How can I make Laravel eloquent relationship with multiple tables?如何使 Laravel 与多个表建立雄辩的关系?
【发布时间】:2021-05-24 14:48:25
【问题描述】:

我正在尝试像这样建立 3 个表的关系,

users
    id
    name
roles
    id
    name
companies
    id
    name
company_role_user
    user_id
    role_id
    company_id

User.php 模型中的关系

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
}

Company.php 模型中的关系

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
}

我想获得这样的特定公司的用户角色

User::find(1)->companies[0]->role->name 

Company::find(1)->users[0]->role->name

【问题讨论】:

  • 公司和用户有很多Roles,你不能像Company::find(1)->users[0]->role->name提供的例子那样只得到一个角色
  • 公司和用户各自拥有许多角色。但是对于特定的公司和特定的用户,只有一个角色。因为 company_role_user 表没有重复的行条目。 @Kloshar4o
  • 如果我在单个项目中获取数组,我没有问题。我可以这样得到,Company::find(1)->users[0]->role[0]->name
  • 看起来您正在尝试“多对多”连接 3 个表,如果可以,您应该考虑简化此操作,很少需要多对多表,使其更简单:用户属于公司, Company hasMany User, User hasMany Role, 所以你可以做类似 User::find(1)->roles, Company::find(1)->user->roles 为什么公司应该有角色?
  • 公司不应该有角色。但是公司用户有一个角色。例如,user1 是 company1 的管理员,user1 是 company2 的成员,user2 是 company1 的成员。这里 admin 和 member 是角色。我怎样才能得到这个?

标签: php laravel eloquent relationship eloquent-relationship


【解决方案1】:

如果你有用户

users
– id
– name

如果你希望他们与公司有多对多的关系,你应该这样做:

companies
– id
– name

user_companies
– user_id
– company_id

对于您的角色,(多对多)您也可以这样做:

roles
 - id
 - name

user_roles
 - user_id
 - role_id

您正在尝试制作多对多 3 张桌子,而您应该使用 2 张桌子。
就算你做到了,也会非常复杂和混乱。
你应该考虑哪些表应该与角色相关,公司应该有角色,或者用户应该有角色,你不需要他们都有角色

【讨论】:

  • 如果我问你 User1 有哪个公司的管理员角色?根据你的数据库设计如何找到它?
【解决方案2】:

您的架构看起来正确,但您的模型定义/关系可以使用连接/枢轴模型CompanyRoleUser,它将这 3 个关系关联为来自 Company/Role/ 的多对一/belongsTo 和一对多/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');
    }
}

现在您可以为您的数据应用不同类型的过滤器,例如

获取公司 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);
     });
 

或者

User::whereHas('companyRoleUser', function ($query) use ($company_id) {
       $query->where('company_id', $company_id)
             ->where('role_id', $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);
     });

【讨论】:

    猜你喜欢
    • 2019-01-10
    • 2019-01-03
    • 1970-01-01
    • 1970-01-01
    • 2017-10-17
    • 2017-04-14
    • 2021-01-25
    • 2017-05-19
    • 2017-10-12
    相关资源
    最近更新 更多