在Laravel 5上获得三级关系 [英] Obtain three level relationship on Laravel 5
问题描述
我试图获取三级关系数据,但是我对使用laravel 5.1感到迷茫
我会尽力解释我的情况,希望您能为我提供帮助.
我有两个名为 Host
和 User
的模型.使用多对多关系,将这些模型按 Hostgroup
和 Usergroup
模型分组.
然后我有一个名为 usergroup_hostgroup_permissions
的表,该表将 Usergroup
与 Hostgroup
关联起来:
+ -------------- + ---------------------- + ------+ ----- + --------------------- + ---------------- +|领域|类型|空|关键默认 |额外|+ -------------- + ---------------------- + ------ + -----+---------------------+----------------+|id |int(10)无符号|否|PRI |NULL |auto_increment ||usergroup_id |int(10)无符号|否|MUL |NULL |||hostgroup_id |int(10)无符号|否|MUL |NULL |||动作|枚举('allow','deny')|否||允许|||启用|tinyint(1)|否||1 ||+ -------------- + ---------------------- + ------ + -----+ --------------------- + ---------------- +
我想获得一个用户组的用户列表,该用户组在此表中与我的主机所属的主机组有关系.
例如:
我的主机_1 属于 DEV_servers
.在 usergroup_hostgroup_permissions
表上,有一个条目允许 developers
访问 DEV_servers
.用户组 developer
具有3个用户 user_1,user_2和user_3 .
有任何提示吗?预先感谢!
为了获取特定主机中的用户列表,您需要使用 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
字段上含糊不清,请尝试将表 hosts
包含在其中该ID也属于,即 hosts.id
.
我假设您已经定义了这些模型的关系,如下所示:
class HostGroup扩展了口才{/** ...*/公共职能hosts(){返回$ this-> hasMany('App \ Host');}公共功能usergroups(){返回$ this-> belongsToMany('App \ UserGroup','usergroup_hostgroup_permissions');}}
class主机扩展口才{/** ...*/公共功能hostgroup(){返回$ this-> belongsTo('App \ HostGroup');}}
class UserGroup扩展了口才{/** ...*/公共职能用户(){返回$ this-> hasMany('App \ User');}公共职能hostgroups(){返回$ this-> belongsToMany('App \ HostGroup','usergroup_hostgroup_permissions');}}
class用户扩展口才{/** ...*/公共功能usergroup(){返回$ this-> belongsTo('App \ UserGroup');}}
希望对您有帮助.
I'm trying to obtain a three level relationship data, but I'm lost about it using laravel 5.1
I'll try to explain my scenario, hope you can help me.
I've got two models called Host
and User
.
This models are grouped by Hostgroup
and Usergroup
models, using a Many To Many relationship.
Then I've got a table called usergroup_hostgroup_permissions
which relation an Usergroup
with a 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 | |
+--------------+----------------------+------+-----+---------------------+----------------+
I'd like to obtain a list of users that belongs to an usergroup with a relation in this table with a hostgroup where my host belongs to.
For example:
My host_1 belongs to DEV_servers
.
On usergroup_hostgroup_permissions
table, there's an entry that allows developers
to access to DEV_servers
.
The usergroup developer
has 3 users user_1, user_2 and user_3.
Any hint? Thanks in advance!
In order to obtain a list of users in a particular host, you need to nest all the underlying relationships (via .
) using a whereHas
method on the User
model. i.e.
$users = User::whereHas('usergroup.hostgroups.hosts', function($q) use ($hostID){
$q->where('id', $hostID);
})->get();
Moreover, if you want to check against whether the user is allowed to access that particular host, then you may chain another where()
to the above as such:
$users = User::whereHas('usergroup.hostgroups.hosts', function($q) use ($hostID){
$q->where('id', $hostID)->where('usergroup_hostgroup_permissions.action', 'allow');
})->get();
Note: If you are warned on an ambiguous id
field, try including the table hosts
to which the id belongs to as well, i.e. hosts.id
.
I am assuming that you have defined the relations for those models as follows:
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');
}
}
Hope it turns out to be helpful.
这篇关于在Laravel 5上获得三级关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!