id 唯一的 Laravel 规则验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47750807/
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 rule validation unique for id
提问by user3074140
I have a locations table which has a client_id foreign key which links to my Client table. I want to add form validation so that when a new location is created is it unique but only for a given client_id. So client one cant have two locations called America but Client one and two can both have America.
我有一个 location 表,它有一个 client_id 外键,它链接到我的 Client 表。我想添加表单验证,以便在创建新位置时它是唯一的,但仅适用于给定的 client_id。所以客户一不能有两个名为美国的地点,但客户一和客户二都可以有美国。
How do you do this with the laravel validation rules
你如何使用 Laravel 验证规则来做到这一点
回答by Mehrdad Dastgir
Check the laravel validationdocumentation. You can specify the table, column and id to ignore.
检查laravel 验证文档。您可以指定要忽略的表、列和 ID。
'email' => 'unique:users,email_address,'.$user->id
You can also add additional where clauses:
您还可以添加额外的 where 子句:
'email' => 'unique:users,email_address,NULL,id,account_id,1'
In the rule above, only rows with an account_id of 1 would be included in the unique check.
在上面的规则中,只有 account_id 为 1 的行才会包含在唯一检查中。
回答by Rits
You can use laravel's built in unique validation as,
您可以使用 Laravel 的内置唯一验证作为,
'client_id' => 'unique:Client table'
and at the time of edit you can pass exception for current row, like
并且在编辑时,您可以为当前行传递异常,例如
'client_id' => 'unique:Client table,client_id,'.$id
I hope it helps, Thank you...
希望能帮到你,谢谢...