代码之家  ›  专栏  ›  技术社区  ›  good_afternoon

涉及三个表的透视表上的Laravel connect数据

  •  0
  • good_afternoon  · 技术社区  · 5 年前

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

    用户:

        public function accounts() {
            return $this->belongsToMany('App\Account', 'account_role_user', 'user_id', 'account_id');
        }
    
        public function roles() {
            return $this->belongsToMany('App\Role', 'account_role_user', 'user_id', 'role_id');
        }
    

    账户

    public function users() {
            return $this->belongsToMany('App\User', 'account_role_user', 'account_id', 'user_id');
        }
    
        public function roles() {
            return $this->belongsToMany('App\Role', 'account_role_user', 'account_id', 'role_id');
        }
    

    角色:

       public function users() {
        return $this->belongsToMany('App\User', 'account_role_user', 'role_id', 'user_id');
       }
    
       public function accounts() {
          return $this->belongsToMany('App\Account', 'account_role_user', 'role_id', 'account_id');
         }
    

    account_role_user

    Cols公司: user_id ,则, role_id ,则, account_id

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

    因此,他们很多都属于一个“Phillies”账户,担任管理员,也可能属于一个“Astros”账户,担任经理。

    很容易查询一个关系,如 App\User::find(2)->accounts App\User::find(2)->roles ,但我在第三段感情上遇到了麻烦。

    目前,我只是尝试查询所有这些内容并显示在页面上。

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


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

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

    集合的第一个索引是:

    Illuminate\Database\Eloquent\Collection {#3024
         all: [
           App\Account {#3069
             id: 1,
             company: "astros",
             created_at: "2020-02-25 12:35:06",
             updated_at: "2020-02-25 12:35:06",
             users: Illuminate\Database\Eloquent\Collection {#3085
               all: [
                 App\User {#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: Illuminate\Database\Eloquent\Relations\Pivot {#3083
                     account_id: 1,
                     user_id: 1,
                   },
                   roles: Illuminate\Database\Eloquent\Collection {#3094
                     all: [
                       App\Role {#3096
                         id: 1,
                         level: "owner",
                         created_at: "2020-02-25 12:32:50",
                         updated_at: "2020-02-25 12:32:50",
                         pivot: Illuminate\Database\Eloquent\Relations\Pivot {#3099
                           user_id: 1,
                           role_id: 1,
                         },
                       },
                     ],
                   },
                 },
                 App\User {#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: Illuminate\Database\Eloquent\Relations\Pivot {#3039
                     account_id: 1,
                     user_id: 2,
                   },
                   roles: Illuminate\Database\Eloquent\Collection {#3091
                     all: [
                       App\Role {#3097
                         id: 2,
                         level: "admin",
                         created_at: "2020-02-25 12:32:50",
                         updated_at: "2020-02-25 12:32:50",
                         pivot: Illuminate\Database\Eloquent\Relations\Pivot {#3088
                           user_id: 2,
                           role_id: 2,
                         },
                       },
                       App\Role {#3100
                         id: 3,
                         level: "manager",
                         created_at: "2020-02-25 12:32:50",
                         updated_at: "2020-02-25 12:32:50",
                         pivot: Illuminate\Database\Eloquent\Relations\Pivot {#3090
                           user_id: 2,
                           role_id: 3,
                         },
                       },
                     ],
                   },
                 },
               ],
             },
           }
    

    数据库中的数据表示为

    alex@fo.com (用户)是 owner (角色)打开 astros (账户)

    jebbush@gmail.com (用户)是 admin (角色)打开 阿童木 (账户)

    jebbush@gmail.com (用户)是 manager (角色)打开 phillies (账户)

    因此,您会注意到,查询从用户获取所有角色,而不考虑帐户。

    所以我想查询显示用户及其角色的帐户。(他们在特定客户中的角色)

    我的模型关系是否正确?应该 Account belongsToMany Role ?

    1 回复  |  直到 5 年前
        1
  •  1
  •   Dilip Hirapara    5 年前

    正如你所说 使用者 贝朗斯托 公司

    以及用途

    App\User::with('company','role')->whereId(2)->first();
    

    这样,您将获得具有公司和角色的用户。

    App\User::with('accounts.roles')->whereId(2)->first();
    

    这样你就可以 (具有帐户的用户) (具有三个角色的帐户) 特定于该用户。

    现在如果你想用它作为 账户 模型

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