分类表有新增和编辑的功能,写了验证,分类名称无法重复,但是都调用同一种验证的时候,编辑如果不修改任何内容提交会显示名称重复,于是在网上找到这么一段验证代码.
public function messages()
{
switch ($this->method()) {
case 'POST':
return [
'name' => 'required|unique:categories|max:255',
'sort' => 'required|integer|between:1,99'
];
case 'PUT':
return [
'name' => 'required|unique:categories,name,' . $this->route('category') . '|max:255',
'sort' => 'required|integer|between:1,99'
];
}
}
其他的都好理解,就是这里加了 . $this->route('category') .
后,编辑就可以正常验证了.这段代码看不懂,请大家帮我解答一下谢谢了.
unique 是支持多个参数的,从文档来看。
这里都 $this->route('category') 即为第三个参数,要排除的值,这个值来源于你的路由占位符: category

文档中也例举了相关的例子。