更新数据错误时忽略用户结果输入
我正在研究: edit.blade.php
用户可以编辑数据,并且控制器的更新方法在此处:
public function update(Request $request, $id)
{
$discountCode = DiscountCode::find($id);
$request->validate([
'code' => 'unique:discount_codes,code'.$discountCode->id,
'started_at' => 'required_if:timespan,TRUE',
'ended_at' => 'required_if:timespan,TRUE',
],
...
}
因此,您可以看到code
必须在discount_codes 表。
因此,我尝试添加:
$ discountCode-> id
,
以忽略当前数据的唯一验证规则,但它无法解决并返回此错误:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'code15' in
'where clause' (SQL: select count(*) as aggregate from
`discount_codes` where `code15` = GIGvJjp4PM)
15 是我的数据行ID。
那么这里出了什么问题?我该如何解决这个问题?
I'm working on an :edit.blade.php
where users can edit data and the update method of Controller goes here:
public function update(Request $request, $id)
{
$discountCode = DiscountCode::find($id);
$request->validate([
'code' => 'unique:discount_codes,code'.$discountCode->id,
'started_at' => 'required_if:timespan,TRUE',
'ended_at' => 'required_if:timespan,TRUE',
],
...
}
So as you can see the code
must be uique in discount_codes
table.
So I tried adding :
$discountCode->id
in order to ignore unique validation rule for the current data but it does not work out and returns this error:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'code15' in
'where clause' (SQL: select count(*) as aggregate from
`discount_codes` where `code15` = GIGvJjp4PM)
15 is my data row id.
So what's going wrong here? How can I solve this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
rule :: class
创建规则并忽略特定的IDUse
Rule::class
to create the rule and ignore specific id