Laravel在包含三个表的数据透视表上连接数据



我有以下模型和数据透视表:

用户:

public function accounts() {
return $this->belongsToMany('AppAccount', 'account_role_user', 'user_id', 'account_id');
}
public function roles() {
return $this->belongsToMany('AppRole', 'account_role_user', 'user_id', 'role_id');
}

账户

public function users() {
return $this->belongsToMany('AppUser', 'account_role_user', 'account_id', 'user_id');
}
public function roles() {
return $this->belongsToMany('AppRole', 'account_role_user', 'account_id', 'role_id');
}

角色:

public function users() {
return $this->belongsToMany('AppUser', 'account_role_user', 'role_id', 'user_id');
}
public function accounts() {
return $this->belongsToMany('AppAccount', 'account_role_user', 'role_id', 'account_id');
}

account_role_user

Cols:user_idrole_idaccount_id

一个用户可以属于许多不同的帐户,并在不同的帐户上具有许多不同的角色。

因此,他们许多人属于一个账户"费城人",扮演管理员的角色,他们也可能属于一个帐户"太空人",担任经理的角色。

查询一个像AppUser::find(2)->accountsAppUser::find(2)->roles这样的关系很容易,但我在第三个关系上遇到了问题。

目前,我只是想查询它们并显示在一个页面上。

因此,我想列出当前用户所属的帐户以及他们在该帐户中的角色。


编辑:Dilip Hirapara提供了以下查询

Account::with('users.roles')->wherehas('users', function($q){
$q->where('users.id','=', 2);
})->get();

集合的第一个索引是:

IlluminateDatabaseEloquentCollection {#3024
all: [
AppAccount {#3069
id: 1,
company: "astros",
created_at: "2020-02-25 12:35:06",
updated_at: "2020-02-25 12:35:06",
users: IlluminateDatabaseEloquentCollection {#3085
all: [
AppUser {#3071
id: 1,
name: "alex",
email: "alex@fo.com",
email_verified_at: null,
created_at: "2020-02-25 12:35:06",
updated_at: "2020-02-25 12:35:06",
pivot: IlluminateDatabaseEloquentRelationsPivot {#3083
account_id: 1,
user_id: 1,
},
roles: IlluminateDatabaseEloquentCollection {#3094
all: [
AppRole {#3096
id: 1,
level: "owner",
created_at: "2020-02-25 12:32:50",
updated_at: "2020-02-25 12:32:50",
pivot: IlluminateDatabaseEloquentRelationsPivot {#3099
user_id: 1,
role_id: 1,
},
},
],
},
},
AppUser {#3084
id: 2,
name: "jeb",
email: "jebbush@gmail.com",
email_verified_at: null,
created_at: "2020-02-25 12:36:57",
updated_at: "2020-02-25 12:36:57",
pivot: IlluminateDatabaseEloquentRelationsPivot {#3039
account_id: 1,
user_id: 2,
},
roles: IlluminateDatabaseEloquentCollection {#3091
all: [
AppRole {#3097
id: 2,
level: "admin",
created_at: "2020-02-25 12:32:50",
updated_at: "2020-02-25 12:32:50",
pivot: IlluminateDatabaseEloquentRelationsPivot {#3088
user_id: 2,
role_id: 2,
},
},
AppRole {#3100
id: 3,
level: "manager",
created_at: "2020-02-25 12:32:50",
updated_at: "2020-02-25 12:32:50",
pivot: IlluminateDatabaseEloquentRelationsPivot {#3090
user_id: 2,
role_id: 3,
},
},
],
},
},
],
},
}

我数据库中的数据表示为

alex@fo.com(用户(是astros(帐户(上的owner(角色(

jebbush@gmail.com(用户(是astros(帐户(上的admin(角色(

jebbush@gmail.com(用户(是phillies(帐户(上的manager(角色(

因此,您会注意到,无论帐户如何,查询都会从用户获取所有角色。

因此,我想查询显示用户及其角色的帐户。(他们在特定账户上的角色(

我的模型关系正确吗?应该AccountbelongsToManyRole吗?

正如您所说,用户属于公司

使用

AppUser::with('company','role')->whereId(2)->first();

通过此操作,您将获得具有公司和角色的用户。

AppUser::with('accounts.roles')->whereId(2)->first();

通过此操作,您将获得(具有帐户的用户(

[具有这些角色的帐户(现在,如果您想通过帐户型号使用它

Account::with('users.roles')->wherehas('users', function($q){
$q->where('id','=', 2);
})->get();

最新更新