简体   繁体   中英

How to define this Laravel relationship

Hello I do have a ER diagram like below. I am thinking this is a hasmanyThrough relationship but I am not sure:

在此处输入图片说明

Any ideas how to define migrations and relationships for those tables ?

in User::class you can use the relation

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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