在 Laravel 5 上获得三级关系
Obtain three level relationship on Laravel 5
我正在尝试获取三级关系数据,但我使用 laravel 5.1
迷失了方向
我会尽量解释我的场景,希望你能帮助我。
我有两个型号 Host
和 User
。
此模型按 Hostgroup
和 Usergroup
模型分组,使用 多对多 关系。
然后我有一个名为 usergroup_hostgroup_permissions
的 table 关系 Usergroup
与 Hostgroup
:
+--------------+----------------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+----------------------+------+-----+---------------------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| usergroup_id | int(10) unsigned | NO | MUL | NULL | |
| hostgroup_id | int(10) unsigned | NO | MUL | NULL | |
| action | enum('allow','deny') | NO | | allow | |
| enabled | tinyint(1) | NO | | 1 | |
+--------------+----------------------+------+-----+---------------------+----------------+
我想获取属于某个用户组的用户列表,该用户组在此 table 中与我的主机所属的主机组有关系。
例如:
我的 host_1 属于 DEV_servers
。
在 usergroup_hostgroup_permissions
table 上,有一个允许 developers
访问 DEV_servers
的条目。
用户组 developer
有 3 个用户 user_1、user_2 和 user_3.
有什么提示吗?提前致谢!
为了获得特定主机中用户的列表,您需要nest 所有底层关系(通过 .
)在 User
模型上使用 whereHas
方法。即
$users = User::whereHas('usergroup.hostgroups.hosts', function($q) use ($hostID){
$q->where('id', $hostID);
})->get();
此外,如果你想检查用户是否被允许到访问该特定主机,那么您可以将另一个 where()
链接到上面:
$users = User::whereHas('usergroup.hostgroups.hosts', function($q) use ($hostID){
$q->where('id', $hostID)->where('usergroup_hostgroup_permissions.action', 'allow');
})->get();
注意: 如果您在不明确的 id
字段上收到警告,请尝试包含 table hosts
id 也属于,即 hosts.id
.
我假设您已经为这些模型定义了如下关系:
class HostGroup extends Eloquent {
/** ...
*/
public function hosts(){
return $this->hasMany('App\Host');
}
public function usergroups(){
return $this->belongsToMany('App\UserGroup', 'usergroup_hostgroup_permissions');
}
}
class Host extends Eloquent {
/** ...
*/
public function hostgroup() {
return $this->belongsTo('App\HostGroup');
}
}
class UserGroup extends Eloquent {
/** ...
*/
public function users(){
return $this->hasMany('App\User');
}
public function hostgroups(){
return $this->belongsToMany('App\HostGroup', 'usergroup_hostgroup_permissions');
}
}
class User extends Eloquent {
/** ...
*/
public function usergroup(){
return $this->belongsTo('App\UserGroup');
}
}
希望对您有所帮助。
我正在尝试获取三级关系数据,但我使用 laravel 5.1
迷失了方向我会尽量解释我的场景,希望你能帮助我。
我有两个型号 Host
和 User
。
此模型按 Hostgroup
和 Usergroup
模型分组,使用 多对多 关系。
然后我有一个名为 usergroup_hostgroup_permissions
的 table 关系 Usergroup
与 Hostgroup
:
+--------------+----------------------+------+-----+---------------------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+----------------------+------+-----+---------------------+----------------+
| id | int(10) unsigned | NO | PRI | NULL | auto_increment |
| usergroup_id | int(10) unsigned | NO | MUL | NULL | |
| hostgroup_id | int(10) unsigned | NO | MUL | NULL | |
| action | enum('allow','deny') | NO | | allow | |
| enabled | tinyint(1) | NO | | 1 | |
+--------------+----------------------+------+-----+---------------------+----------------+
我想获取属于某个用户组的用户列表,该用户组在此 table 中与我的主机所属的主机组有关系。
例如:
我的 host_1 属于 DEV_servers
。
在 usergroup_hostgroup_permissions
table 上,有一个允许 developers
访问 DEV_servers
的条目。
用户组 developer
有 3 个用户 user_1、user_2 和 user_3.
有什么提示吗?提前致谢!
为了获得特定主机中用户的列表,您需要nest 所有底层关系(通过 .
)在 User
模型上使用 whereHas
方法。即
$users = User::whereHas('usergroup.hostgroups.hosts', function($q) use ($hostID){
$q->where('id', $hostID);
})->get();
此外,如果你想检查用户是否被允许到访问该特定主机,那么您可以将另一个 where()
链接到上面:
$users = User::whereHas('usergroup.hostgroups.hosts', function($q) use ($hostID){
$q->where('id', $hostID)->where('usergroup_hostgroup_permissions.action', 'allow');
})->get();
注意: 如果您在不明确的 id
字段上收到警告,请尝试包含 table hosts
id 也属于,即 hosts.id
.
我假设您已经为这些模型定义了如下关系:
class HostGroup extends Eloquent {
/** ...
*/
public function hosts(){
return $this->hasMany('App\Host');
}
public function usergroups(){
return $this->belongsToMany('App\UserGroup', 'usergroup_hostgroup_permissions');
}
}
class Host extends Eloquent {
/** ...
*/
public function hostgroup() {
return $this->belongsTo('App\HostGroup');
}
}
class UserGroup extends Eloquent {
/** ...
*/
public function users(){
return $this->hasMany('App\User');
}
public function hostgroups(){
return $this->belongsToMany('App\HostGroup', 'usergroup_hostgroup_permissions');
}
}
class User extends Eloquent {
/** ...
*/
public function usergroup(){
return $this->belongsTo('App\UserGroup');
}
}
希望对您有所帮助。