dongyouzhi7218 2015-08-02 18:41
浏览 83
已采纳

在Laravel 5上获得三级关系

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!

  • 写回答

1条回答 默认 最新

  • doujiongqin0687 2015-08-03 11:35
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?