复合唯一键验证 - laravel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29093061/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:09:02  来源:igfitidea点击:

composite-unique-key-validation - laravel

phpvalidationlaravel

提问by sumit

seems it's not possible with built-in validators, how I should implement this feature in the model?

内置验证器似乎不可能,我应该如何在模型中实现这个功能?

 $rules = [

            'user_id' => 'required|unique:service_details,user_id',
            'service_id'=>'required|unique:service_details,service_id'
         ];

above will prevent duplicacy of user_idand service_idindependently which is not my requirement

以上将防止重复user_idservice_id独立,这不是我的要求

it will reject

它会拒绝

(1,2)
(1,3)

because 1 is duplicate but it should be accepted as i want composite unique key

因为 1 是重复的,但它应该被接受,因为我想要复合唯一键

回答by harryg

Composite unique field validation is provided out-of-the-box in 5.0+. I can't speak for earlier versions.

复合唯一字段验证在 5.0+ 中开箱即用。我不能说早期版本。

You can essentially specify a where clause for when your unique validation rule applies. E.g.

您基本上可以为何时应用您的唯一验证规则指定一个 where 子句。例如

'term'  => 'unique:terms,term,NULL,id,taxonomy,category'

This rule says that termshould be unique on the termstable but only where the taxonomyis equal to "category".

这条规则说termterms表上应该是唯一的,但只有在taxonomy等于“类别”的地方。

For example, it will prevent a "news" category being duplicated but I can still have a "news" tag.

例如,它会防止重复“新闻”类别,但我仍然可以拥有“新闻”标签。



I don't know your schema but in your case it'd be something like this:

我不知道你的架构,但在你的情况下,它会是这样的:

$user_id = Request::get('user_id', $default);
$service_id = Request::get('service_id', $default);  
// or another way of specifying the accompanying service/user ID for the where clauses

$rules = [
    'user_id' => 'unique:service_details,user_id,NULL,id,service_id,' . $service_id;
    'service_id' => 'unique:service_details,service_id,NULL,id,user_id,' . $user_id;
];

回答by nozzleman

This is not possible out of the box in Laravel. You would either have to write a custom validatoror use a package for that.

这在 Laravel 中开箱即用是不可能的。您要么必须编写自定义验证器,要么为此使用包。

Here is one that should do what you need: - https://github.com/felixkiss/uniquewith-validator

这是一个应该做你需要的: - https://github.com/felixkiss/uniquewith-validator

With this package, your rules could look like the following:

使用此包,您的规则可能如下所示:

$rules = array(
    'user_id' => 'required|unique_with:service_details,service_id',
    'service_id' => 'required',
);

It works for both Laravel 4 and 5.

它适用于 Laravel 4 和 5。

回答by ErcanE

My solution

我的解决方案

Laravel 5.3 and 5.4

Laravel 5.3 和 5.4

Note: Without model binding

注意:无模型绑定

Store

店铺

'required',
Rule::unique('service_details','service_id')->where(function ($query) {
      $query->where('user_id', $this->request->get('user_id'));
})

Update

更新

'required',
Rule::unique('service_details','service_id')->ignore($this->route()->id)->where(function ($query) {
      $query->where('user_id', $this->request->get('user_id'));
})

回答by s vinayagam

With the help of this link, I am able to achieve composite validation from out of the box laravel 5.3. I knew it is old question and answered already. In my case vendor and client combination is unique.

在此链接的帮助下,我能够从开箱即用的 laravel 5.3 中实现复合验证。我知道这是个老问题并且已经回答了。就我而言,供应商和客户的组合是独一无二的。

My sample code is below

我的示例代码如下

 $this->validate($request, [
        'Vendor'=> 'bail|required|int',
        'Client'=> 'bail|required|int',      
        'discount'=> 'required|numeric',            
        'Client'=>'unique:billings,client_id,vendor_id'.$request->Vendor
    ]);