laravel:如何获取与数据透视表中的另一列相关的列(3列数据透视) [英] laravel: How to get column related to another column in pivot table (3 column pivot)

查看:71
本文介绍了laravel:如何获取与数据透视表中的另一列相关的列(3列数据透视)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定如何表达这个问题,但是我需要执行以下操作:

I'm not sure how to phrase this question, but I need to do the following:

我有一个包含三列的数据透视表 user_id account_id role_id

I have a pivot table with three columns user_id, account_id, role_id

所以在我的数据库中,如果我在 account_role_user (数据透视)上有这些行

So in my database if I have these rows on account_role_user (pivot)

|---------------------|------------------|-------------
|     account_id      |     user_id      |   role_id  |
|---------------------|------------------|-------------
|          1          |         1        |     1      |
|---------------------|--------------------------------
|          2          |         2        |     1       |
------------------------------------------------------
|          1          |         2        |     3      |
------------------------------------------------------

角色-1 =所有者

角色-3 =经理

我想做的是让用户使用其特定角色的特定帐户.但是我只能查询类似 App \ User :: with('accounts.roles')-> whereId(2)-> first(); 之类的东西,它确实向用户显示一个帐户,但将其不是特定于该帐户的角色分组.

What I'm trying to do is get users on a specific account with their specific role. But I'm only able to query things like App\User::with('accounts.roles')->whereId(2)->first(); which does show the users on an account, but it groups their roles that aren't specific to the account.

我只希望与帐户相关的用户及其角色.

I only want the users and their roles that are relevant to the account.

如果我要查询ID为1的帐户,并且想以该角色列出该帐户上的用户,则应该有以下内容:

If I'm querying account with id of 1 and I want to list users on that account with their role, I should have something like:

$ account = App \ Account :: find(1)

ID为1的用户在帐户ID为1时的角色ID为1

ID为2的用户在帐户ID为1时的角色ID为3

用户模型:

    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');
     }

推荐答案

感谢Laravel的Discord频道上某人的帮助,我明白了.

Thanks to someone's help on Laravel's Discord channel, I figured this out.

Account 模型上,我将 users()关系更改为:

On the Account Model, I changed the users() relation to:

    public function users() {
        return $this->belongsToMany('App\User', 'account_role_user')
        ->using('App\AccountRoleUser')
        ->withPivot('role_id');
    }

我创建了一个名为 AccountRoleUser

namespace App;


use Illuminate\Database\Eloquent\Relations\Pivot;

class AccountRoleUser extends Pivot
{
    public function role() {
        return $this->belongsTo('App\Role');
    }
}

User 模型上,我将 accounts()关系更改为:

And on the User model, I changed the accounts() relation to:

    public function accounts() {
        return $this->belongsToMany(Account::class, 'account_role_user')->withPivot('role_id');
    }

现在我的查询确实很简洁和合乎逻辑:

Now my query is really succinct and logical:

$account = auth()->user()->accounts->where('foo', 'bar')->first();
$users = $account->users;

集合中的每个用户现在都有一个来自数据透视模型的 pivot 对象.

Each User inside the collection has a pivot object now coming from the pivot model.

这意味着我现在可以做类似的事情:

Which means I can now do something like:

@foreach ($users as $user)
  <p>This is user {{ $user->first_name }} {{ $user->last_name }} with a role of 
   {{$user->pivot->role->description}} 
</p>
@endforeach

这篇关于laravel:如何获取与数据透视表中的另一列相关的列(3列数据透视)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆