【发布时间】:2019-02-26 12:33:08
【问题描述】:
我正在使用 laravel 5.7 并使用表单请求,但我的数据库中有一个唯一的 branch_name 以及唯一的 slug,但每次更新验证错误 .branch 名称已经存在
public function rules()
{
switch($this->method()) {
case 'DELETE':
return [
'slug'=>'required',
];
case 'POST':
case 'PUT':
case 'PATCH':
return [
'branch_name'=>'required|max:255|unique:branches,branch_name,id'.$this->id,
'branch_address'=>'required',
];
default:
break;
}
}
我也尝试了以下但没有用
'branch_name'=>'required|max:255|unique:branches,branch_name,'.$this->id,
和
'branch_name'=>'required|max:255|unique:branches,branch_name,slug'.$this->slug,
即使我在规则方法上打印了 id 和 slug,我也有一个隐藏值,我可以看到 id 和 slug
【问题讨论】:
-
有两列一是分支,一是分支名称这是什么?
-
@Gurpalsingh.branches 是表名
-
'branch_name'=>'required|max:255|unique:branches,branch_name,'.$this->id.',id' -
$this->id是否等于数据库中要更新的列的id?! -
@MohamedEmad.yes 你是正确的它的主键