Laravel:更新和创建时验证唯一
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/48666486/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel: Validation unique when update and create
提问by Maria Camila Cuadrado
I have a Request with rules where I validate that one field is unique.
我有一个带有规则的请求,我验证一个字段是唯一的。
When I use:
当我使用:
'cod_upb' => 'required|max:10|unique:equipos,cod_upb,'.$this->id,
It works in the create method because it doesn't allow me to repeat the field in the database. But when I try to update it says that the field already exist or is taken.
它在 create 方法中有效,因为它不允许我重复数据库中的字段。但是当我尝试更新时,它说该字段已经存在或被占用。
Then I tried with:
然后我尝试:
'cod_upb' => 'required|max:10|unique:equipos,id,'.$this->id,
Which works on the update, but it let me create new items repeating the field.
这适用于更新,但它让我创建重复该字段的新项目。
How can I fix that?
我该如何解决?
回答by Niklesh Raut
In UniqueRule
在独特的规则
unique:table,column,except,idColumn
唯一:表,列,除外,idColumn
3rd param is for value for column to except
and 4th is for column to except
第三个参数用于value for column to except
,第四个参数用于column to except
I am also using one validation method for both insert
and update
like this way
我也使用一种验证方法insert
,update
就像这样
public function validateItems($requestAll){
$id = isset($requestAll['id']) ? ','.$requestAll['id'].',id':'';
$rules = [
'cod_upb' => 'required|max:10|unique:equipos,cod_upb,'.$id,
];
return Validator::make($requestAll, $rules);
}
Just change code for $requestAll['id']
as I took for id
, in your case it might be $this->id
只需$requestAll['id']
按照我的方式更改代码id
,就您而言,它可能是$this->id
回答by RashedRahat
Here is the solution:
这是解决方案:
For create:
对于创建:
public function controllerName(Request $request, $id)
public function controllerName(Request $request, $id)
{
{
$this->validate($request, [
"form_field_name" => 'required|unique:db_table_name,db_table_column_name'
]);
// the rest code
}
}
For Update:
更新:
public function anotherControllerName(Request $request, $id)
public function anotherControllerName(Request $request, $id)
{
{
$this->validate($request, [
"form_field_name" => 'required|unique:db_table_name,db_table_column_name,'.$id
]);
// the rest code
}
}
That's it. Happy Coding :)
就是这样。快乐编码:)
回答by tprj29
You are checking the unique on update wrong. You are checking it against the id field instead of the cod_upb field.
$this->id
should be renamed to something like $equiposId
. Make sure $this->id
is the id representing the equipos
record in the database.
您正在检查更新时的唯一性错误。您正在根据 id 字段而不是 cod_upb 字段检查它。
$this->id
应该重命名为类似$equiposId
. 确保$this->id
id 代表equipos
数据库中的记录。
Change this:
改变这个:
'cod_upb' => 'required|max:10|unique:equipos,id,'.$this->id,
To this:
对此:
'cod_upb' => 'required|max:10|unique:equipos,cod_upb,'.$this->id,
On laravel 5.5
在 Laravel 5.5 上
'cod_upb' => Rule::unique('equipos')->ignore($this->id),
Read more about this in the Laravel docs: https://laravel.com/docs/5.5/validation#rule-unique
在 Laravel 文档中阅读更多相关信息:https://laravel.com/docs/5.5/validation#rule-unique