无法使用belongsToMany同步数据透视表数据
我有一个名为 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_riskfield
的pivot_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:
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
aspivot_id
frominvite_riskarea_riskfield
inner joininvites
oninvite_riskarea_riskfield
.id
=invites
.id
whereinvites
.id
= 17)
What I did wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
假设您的模型为:
Invite
(invites
表)RiskAreaRiskField
(riskarea_riskfield
表)InviteRiskareaRiskfield
(invite_riskarea_riskfield
表)。你们的关系应该如下。请随意将方法名称更改为对您更有意义的名称。
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.