如果 id 相同,Laravel 验证唯一性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36574583/
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 validate unique if id is the same
提问by Kaizokupuffball
I have a table/model that contains multiple albums per user.
Is there a way to say that the column title
should be unique, but only for the rows that have the same user_id
?
我有一个表/模型,其中包含每个用户的多个专辑。有没有办法说该列title
应该是唯一的,但仅限于具有相同的行user_id
?
Example: http://pastebin.com/8dvM4a1T
示例:http: //pastebin.com/8dvM4a1T
As you can see in the example, the user with the id of 2 has created 2 albums, with the same title. I don't want that to be allowed, that's why I'm wondering if there's a way to deny that with the validator from Laravel?
正如您在示例中看到的,id 为 2 的用户创建了 2 个具有相同标题的相册。我不希望这被允许,这就是为什么我想知道是否有办法通过 Laravel 的验证器来否认这一点?
I tried this, but that did not work.
我试过这个,但那没有用。
// Validator
$validator = Validator::make($input, [
'title' => 'required|min:1|max:255|unique:galleries,title,'. Auth::user() -> id .',user_id',
'description' => 'min:1|max:255'
]);
Any help appreciated, thanks.
任何帮助表示赞赏,谢谢。
回答by VipindasKS
Your code should be something like:
你的代码应该是这样的:
'title' => 'unique:galleries,title,NULL,id,user_id,'.Auth::user() -> id.'',
Or, you can write a custom rule Reference here
回答by Bogdan
The approach with the default unique
rule does not work because the rule expects the column value to be passed as the third parameter, so in your case it would check if the title
column is equal to the Auth::user()->id
value which is not what you want.
使用默认unique
规则的方法不起作用,因为该规则期望将列值作为第三个参数传递,因此在您的情况下,它会检查该title
列是否等于Auth::user()->id
不是您想要的值。
You can create you own custom validation rule by adding the following code to the boot
method of the App\Providers\AppServiceProvider
class:
您可以通过将以下代码添加到类的boot
方法中来创建自己的自定义验证规则App\Providers\AppServiceProvider
:
Validator::extend('unique_custom', function ($attribute, $value, $parameters)
{
// Get the parameters passed to the rule
list($table, $field, $field2, $field2Value) = $parameters;
// Check the table and return true only if there are no entries matching
// both the first field name and the user input value as well as
// the second field name and the second field value
return DB::table($table)->where($field, $value)->where($field2, $field2Value)->count() == 0;
});
Now you can use the unique_custom
(or you can name it whatever you like) rule like so:
现在您可以使用unique_custom
(或者您可以随意命名)规则,如下所示:
$validator = Validator::make($input, [
'title' => 'required|min:1|max:255|unique_custom:galleries,title,user_id,' . Auth::id(),
'description' => 'min:1|max:255'
]);
The rule expects the parameters to be the following:
该规则期望参数如下:
- the 1st parameterto be the table name, which in this case is
galleries
- the 2nd parameterto be the field name that is supposed to be unique and for which the value comes from the user input, which in this case is
title
- the 3rd parameterto be the second field name that will be added to the query conditions, which in this case is
user_id
- the 4th parameterto be the value of the field name passed as the third parameter
- 第一个参数是表名,在这种情况下是
galleries
- 该第二参数是指被认为是独特的和的量,值来自用户的输入,在此情况下是字段名称
title
- 所述第三参数来是将被添加到查询条件的第二字段名称,在这种情况下是
user_id
- 在第四个参数是作为第三个参数传递的域名的价值
Also you can use Auth::id()
since that is the short form of Auth::user()->id
.
您也可以使用,Auth::id()
因为这是Auth::user()->id
.
You can read more about Custom Validation rules in the Laravel Documentation.
您可以在Laravel 文档 中阅读有关自定义验证规则的更多信息。