【问题标题】:Laravel Eloquent for pivot table with 2 foreign keys to a table and 1 foreign key to another tableLaravel Eloquent 用于数据透视表,一个表有 2 个外键,另一个表有 1 个外键
【发布时间】:2020-12-30 18:34:49
【问题描述】:

我有如下表,其中role_idroles 表的外键,user_idsetter_idusers 表的外键。

table 1:
+---------------------+
| users               |
+---------------------+
| id                  |
| name                |
| email               |
| password            |
+---------------------+

table 2:
+---------------------+
| roles               |
+---------------------+
| id                  |
| name                |
+---------------------+

pivot table:
+---------------------+
| role_user           |
+---------------------+
| role_id             |
| user_id             |
| setter_id           |
+---------------------+

我定义的模型:

用户模型:

class User extends Model
{
    public $timestamps = false;
    
     public function roles()
     {
         return $this->belongsToMany(Role::class);
     }
}

榜样:

class Role extends Model
{
    public $timestamps = false;
    
    public function users()
    {
        return $this->belongsToMany(User::class);
    }
}

如何更改我的模型,以便获得如下所示的数据?

user -> roles -> setter : 用户及其角色以及用户每个角色的 setter

谢谢...

【问题讨论】:

标签: php laravel eloquent pivot-table laravel-7


【解决方案1】:

您将永远无法通过在角色集合中调用 setter 来访问它。

这是错误的:

$user->roles->setter

让我们看一个可行的例子:

foreach($user->roles as $role)
{
    dd($role->pivot->setter)
}

要做到这一点,您需要更改模型以反映如下内容:

用户

class User extends Model
{
    public $timestamps = false;
    
     public function roles()
     {
         return $this->belongsToMany(Role::class)
            ->using(UserRolePivot::class)
            ->withPivot([
                'role_id',
                'user_id',
                'setter_id',
            ]);
     }
}

角色

class Role extends Model
{
    public $timestamps = false;
    
    public function users()
    {
        return $this->belongsToMany(User::class)
            ->using(UserRolePivot::class)
            ->withPivot([
                'role_id',
                'user_id',
                'setter_id',
            ]);
    }
}

枢轴

use Illuminate\Database\Eloquent\Relations\Pivot;

class UserRolePivot extends Pivot
{
    protected $fillable = [
        'role_id',
        'user_id',
        'setter_id',
    ];

    public function role()
    {
        return $this->belongsTo(Role::class , 'role_id');
    }

    public function user()
    {
        return $this->belongsTo(User::class , 'user_id');
    }

    public function setter()
    {
        return $this->belongsTo(User::class , 'setter_id);
    }
}

【讨论】:

    【解决方案2】:

    您可以更新 belongsToMany 调用以在枢轴上包含 setter_id,然后通过 ->pivot->setter_id 访问它并使用该 id 检索模型。

    return $this->belongsToMany(Role::class)->withPivot('setter_id');
    

    或者(以及我个人会选择的)您可以define a custom pivot model,并在那里创建setter() 关系,这样您就可以直接从枢轴检索模型。

    【讨论】:

      猜你喜欢
      • 2015-02-02
      • 1970-01-01
      • 1970-01-01
      • 2019-01-29
      • 2015-01-16
      • 2020-03-28
      • 1970-01-01
      • 2022-01-19
      • 2018-07-06
      相关资源
      最近更新 更多