简体   繁体   English

在Laravel 5上获得三级关系

[英]Obtain three level relationship on Laravel 5

I'm trying to obtain a three level relationship data, but I'm lost about it using laravel 5.1 我正在尝试获取三级关系数据,但是使用laravel 5.1迷失了它

I'll try to explain my scenario, hope you can help me. 我将尽力解释我的情况,希望您能帮助我。

I've got two models called Host and User . 我有两个名为HostUser模型。 This models are grouped by Hostgroup and Usergroup models, using a Many To Many relationship. 这个模型是由分组HostgroupUsergroup的机型,使用多对多的关系。

Then I've got a table called usergroup_hostgroup_permissions which relation an Usergroup with a Hostgroup : 然后,我有一个表叫usergroup_hostgroup_permissions其关系的UsergroupHostgroup

+--------------+----------------------+------+-----+---------------------+----------------+
| 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 . 我的host_1属于DEV_servers On usergroup_hostgroup_permissions table, there's an entry that allows developers to access to DEV_servers . usergroup_hostgroup_permissions表上,有一个条目允许developers访问DEV_servers The usergroup developer has 3 users user_1, user_2 and user_3 . 用户组developer具有3个用户user_1,user_2和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. 为了获得特定主机中的用户列表,您需要使用User模型上的whereHas方法嵌套所有基础关系(通过. )。 ie

$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: 此外,如果要检查是否允许用户访问该特定主机,则可以这样将另一个where()到上面:

$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, ie hosts.id . 注意:如果在id字段不明确的情况下收到警告,请尝试包括该ID所属的表hosts ,即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. 希望对您有所帮助。

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

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