Laravel 验证:存在两列同一行

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

Laravel validation: exists two column same row

phplaravelvalidationrules

提问by Miloud BAKTETE

Is there a way of referencing another field when specifying the exists validation rule in Laravel?

在 Laravel 中指定存在验证规则时,是否可以引用另一个字段?

My request :

我的请求 :

public function rules()
    {

        return [
            'numero_de_somme' => 'unique:personnels,numero_de_somme|exists:fonctionnaire,num_somme',
            'cin' => 'unique:personnels,cin|exists:fonctionnaire,cin',

        ];
    }

in my validation rules I want to be able to make sure that:

在我的验证规则中,我希望能够确保:

  • num_sommeexists within the fonctionnairetable
  • cinexists within the fonctionnaire table and cininput must be on the same row of the num_somme
  • num_somme存在于fonctionnaire表中
  • cin存在于 fonctionnaire 表中,并且cin输入必须在num_somme

num_somme : 12345 cin : s89745

num_somme : 12345 cin : s89745

num_somme : 78945 cin : U10125

num_somme : 78945 cin : U10125

Explaining : for example

解释:例如

  • 1st scenario if the input num_somme= 12345 and cin= U10125 the validation must fail
  • 2nd scenario if the input num_somme= 12345 and cin= s89745 the validation must success
  • 第一种情况,如果输入num_somme= 12345 且cin= U10125,则验证必须失败
  • 第二种情况如果输入num_somme= 12345 和cin= s89745 验证必须成功

I hope this makes sense.

我希望这是有道理的。

Thank you

谢谢

回答by roerjo

I came across the same need today and I think I have a solution using the validation Rule class: Rule example.

我今天遇到了同样的需求,我想我有一个使用验证规则类的解决方案:规则示例

Here is my scenario: I have an email verifications table and I want to ensure that a passed machine code and activation code exist on the same row.

这是我的场景:我有一个电子邮件验证表,我想确保在同一行中存在传递的机器代码和激活代码。

Be sure to include use Illuminate\Validation\Rule;

务必包括 use Illuminate\Validation\Rule;

$activationCode = $request->activation_code;                                   

$rules = [                                                                     
    'mc' => [                                                                  
        'required',                                                            
        Rule::exists('email_verifications', 'machineCode')                     
        ->where(function ($query) use ($activationCode) {                      
            $query->where('activationCode', $activationCode);                  
        }),                                                                    
    ],                                                                         
    'activation_code' => 'required|integer|min:5',                             
    'operating_system' => 'required|alpha_num|max:45'                          
];

The first argument in the exists method is the table and the second is the custom column name I'm using for the 'mc' field. I pass the second column I want to check using the 'use' keyword and then use that field in a a where clause.

exists 方法中的第一个参数是表,第二个参数是我用于 'mc' 字段的自定义列名。我通过了我想使用“use”关键字检查的第二列,然后在 aa where 子句中使用该字段。

回答by Anandan K

You can achieve validating multipe columns by using Rule.

您可以使用Rule来验证多列。

Include :

包括 :

use Illuminate\Validation\Rule;

use Illuminate\Validation\Rule;

use Validator;

use Validator;

$AdminId = $request->AdminId;
$Validate = Validator::make($request->all(),[
        'TaxId' => [ 'required', Rule::exists('tax','id')                     
                                    ->where(function ($query) use ($AdminId) {                      
                                    $query->where('u_id', $AdminId);                  
                                    })], 
    ]);
if($Validate->fails())
        {`Your ERROR HERE`}
else
        {`SUCCESS`}

Here in my example I'm checking u_id and Id which is in same row and 2 columns.

在我的示例中,我正在检查同一行和 2 列中的 u_id 和 Id。

Explaining : for example

解释:例如

My DB HAS 2 ROWS IN tax TABLE

我的数据库在税表中有 2 行

id = 1 , u_id = 1, tax = "GST" , value -> 8

id = 1 , u_id = 1, tax = "GST" , value -> 8

id = 2 , u_id = 2, tax = "GST" , value -> 5

id = 2 , u_id = 2, tax = "GST" , value -> 5

1st scenario if the input id = 2 and u_id = 1the validation must failbecause this row belongs to u_id = 2.

第一种情况,如果输入id = 2 且 u_id = 1,则验证必须失败,因为该行属于 u_id = 2。

2nd scenario if the input id = 2 and u_id = 2the validation must success

第二种情况,如果输入id = 2 且 u_id = 2验证必须成功

回答by AliN11

You can simply use this:

你可以简单地使用这个:

'request_input' => exists:table,col,alternative_col_1,alternative_val_1,alternative_col_2,alternative_val_2, ...'

The SQL query is like below:

SQL 查询如下所示:

select count(*) as aggregate from `table` where `id` = [request_input] and `alternative_col1` = "alternative_val1" and `alternative_col2` = "alternative_val2"

回答by Miloud BAKTETE

i managed to solve it using the most simple method

我设法用最简单的方法解决了它

  • for numero_de_somme
  • 为了 numero_de_somme

'numero_de_somme' => 'unique:personnels,numero_de_somme|exists:fonctionnaire,num_somme,cin,'.Request::get('cin'),

'numero_de_somme' => 'unique:personnels,numero_de_somme|exists:fonctionnaire,num_somme,cin,'.Request::get('cin'),

  • for cin
  • 为了 cin

'cin' => 'unique:personnels,cin|exists:fonctionnaire,cin',

'cin' => 'unique:personnels,cin|exists:fonctionnaire,cin',

PS. don't forget to call use Illuminate\Http\Request;

附注。不要忘记打电话use Illuminate\Http\Request;