简体   繁体   English

如何定义这种Laravel关系

[英]How to define this Laravel relationship

Hello I do have a ER diagram like below. 您好,我确实有如下所示的ER图。 I am thinking this is a hasmanyThrough relationship but I am not sure: 我以为这是一个hasmanyThrough关系,但我不确定:

在此处输入图片说明

Any ideas how to define migrations and relationships for those tables ? 有什么想法如何定义这些表的迁移和关系?

in User::class you can use the relation 在User :: class中,您可以使用关系

public function organizations(){
    return $this->belongsToMany(
        Organization::class ,
        'organization_user',
        'user_id', //user key on intermediate table
        'organization_id' //organization key on intermediate table
    );
}

public function roles(){
    return $this->belongsToMany(
        Role::class ,
        'organization_user',
        'user_id', //user key on intermediate table
        'role_id' //role key on intermediate table
    );
}

more info on laravel many-to-many 关于laravel多对多的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM