无法使用belongsToMany同步数据透视表数据

发布于 2025-01-14 23:02:46 字数 1049 浏览 1 评论 0原文

我有一个名为 invite_riskarea_riskfield 的数据透视表,它定义了与 invite 表的关系:

在此处输入图像描述

我需要做的是同步多个invite_riskarea_riskfield 权限(插入、编辑、查看)。所以我尝试在 Invite 模型中设置以下关系:

public function permissions()
{
    return $this->belongsToMany(
        InviteRiskareaRiskfield::class,
        'invites',
        'id',
        'id'
    );
}

所以我应该能够执行 $invite->permissions()->sync($permissions);

但关系返回此错误:

SQLSTATE[42S22]:未找到列:1054“on 子句”中的未知列“invite_riskarea_riskfield.id”(SQL:选择 invite_riskarea_riskfield.*,invites。<代码>id作为来自invite_riskarea_riskfieldpivot_id invite_riskarea_riskfield.id = invites.id 上的内部联接 invites,其中 邀请id = 17)

我做错了什么?

I have a pivot table called invite_riskarea_riskfield which defined a relationship with the invite table:

enter image description here

What I need to do is sync multiple invite_riskarea_riskfield permissions (insert, edit, view). So I tried to set the following relationship in the Invite model:

public function permissions()
{
    return $this->belongsToMany(
        InviteRiskareaRiskfield::class,
        'invites',
        'id',
        'id'
    );
}

So I should be able to do $invite->permissions()->sync($permissions);

But the relationship returns this error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column 'invite_riskarea_riskfield.id' in 'on clause' (SQL: select invite_riskarea_riskfield.*, invites.id as pivot_id from invite_riskarea_riskfield inner join invites on invite_riskarea_riskfield.id = invites.id where invites.id = 17)

What I did wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

双马尾 2025-01-21 23:02:46

假设您的模型为:

  • Inviteinvites 表)
  • RiskAreaRiskFieldriskarea_riskfield 表)
  • InviteRiskareaRiskfieldinvite_riskarea_riskfield 表)。

你们的关系应该如下。请随意将方法名称更改为对您更有意义的名称。

class Invite extends Model
{
    public function invite_riskarea_riskfield()
    {
        return $this->hasMany(InviteRiskAreaRiskField::class, 'invite_id');
    }

    public function riskarea_riskfield()
    {
        return $this->belongsToMany(RiskAreaRiskField::class, 'invite_riskarea_riskfield', 'invite_id', 'riskarea_riskfield_id')
                    ->withPivot(['insert', 'edit', 'view'])
                    ->using(InviteRiskAreaRiskField::class);
    }
}
class RiskAreaRiskField extends Model
{
    public function invite_riskarea_riskfield()
    {
        return $this->hasMany(InviteRiskAreaRiskField::class, 'riskarea_riskfield_id');
    }

    public function invite()
    {
        return $this->belongsToMany(Invite::class, 'invite_riskarea_riskfield', 'riskarea_riskfield_id', 'invite_id')
                    ->withPivot(['insert', 'edit', 'view'])
                    ->using(InviteRiskAreaRiskField::class);
    }
}
use Illuminate\Database\Eloquent\Relations\Pivot;

class InviteRiskareaRiskfield extends Pivot
{
    public function invite()
    {
        return $this->belongsTo(Invite::class, 'invite_id');
    }

    public function riskarea_riskfield()
    {
        return $this->belongsTo(RiskAreaRiskField::class, 'riskarea_riskfield_id');
    }
}
$invite = Invite::find(/* some id */);

// sync with riskarea_riskfield id 1, 2 and 3
$invite->riskarea_riskfield() // the belongsToMany relationship
    ->sync([
        1, // default values for insert, edit, view
        2 => ['insert' => 432, 'edit' => 13, 'view' => 542],
        3 => ['insert' => 654, 'edit' => 777, 'view' => 222]
    ]);


// sync with riskarea_riskfield id 4, 5 and 6 with the same permissions
$invite->riskarea_riskfield() // the belongsToMany relationship
    ->syncWithPivotValues(
        [4, 5, 6],
        ['insert' => 654, 'edit' => 777, 'view' => 222]
    );

Assuming your models are:

  • Invite (invites table)
  • RiskAreaRiskField (riskarea_riskfield table)
  • InviteRiskareaRiskfield (invite_riskarea_riskfield table).

Your relationship should be the following. Feel free to change the method names to something that makes more sense to you.

class Invite extends Model
{
    public function invite_riskarea_riskfield()
    {
        return $this->hasMany(InviteRiskAreaRiskField::class, 'invite_id');
    }

    public function riskarea_riskfield()
    {
        return $this->belongsToMany(RiskAreaRiskField::class, 'invite_riskarea_riskfield', 'invite_id', 'riskarea_riskfield_id')
                    ->withPivot(['insert', 'edit', 'view'])
                    ->using(InviteRiskAreaRiskField::class);
    }
}
class RiskAreaRiskField extends Model
{
    public function invite_riskarea_riskfield()
    {
        return $this->hasMany(InviteRiskAreaRiskField::class, 'riskarea_riskfield_id');
    }

    public function invite()
    {
        return $this->belongsToMany(Invite::class, 'invite_riskarea_riskfield', 'riskarea_riskfield_id', 'invite_id')
                    ->withPivot(['insert', 'edit', 'view'])
                    ->using(InviteRiskAreaRiskField::class);
    }
}
use Illuminate\Database\Eloquent\Relations\Pivot;

class InviteRiskareaRiskfield extends Pivot
{
    public function invite()
    {
        return $this->belongsTo(Invite::class, 'invite_id');
    }

    public function riskarea_riskfield()
    {
        return $this->belongsTo(RiskAreaRiskField::class, 'riskarea_riskfield_id');
    }
}
$invite = Invite::find(/* some id */);

// sync with riskarea_riskfield id 1, 2 and 3
$invite->riskarea_riskfield() // the belongsToMany relationship
    ->sync([
        1, // default values for insert, edit, view
        2 => ['insert' => 432, 'edit' => 13, 'view' => 542],
        3 => ['insert' => 654, 'edit' => 777, 'view' => 222]
    ]);


// sync with riskarea_riskfield id 4, 5 and 6 with the same permissions
$invite->riskarea_riskfield() // the belongsToMany relationship
    ->syncWithPivotValues(
        [4, 5, 6],
        ['insert' => 654, 'edit' => 777, 'view' => 222]
    );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文