Laravel中用于连接两列的验证规则 [英] Validation rules in Laravel for joining two columns

查看:54
本文介绍了Laravel中用于连接两列的验证规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有一种方法可以在Laravel中建立一个验证,以检查表中的两列在一起是否唯一?

例如,如果我的表中有[(A,B)],则输入(A,A)或(B,B)应该通过验证,但(A,B)再次必须失败./p>

例如这样的

  DB :: table('mytable')->其中([['col1',$ item-> col1],['col2',$ item-> col2]])-> exists(); 

但是我想在验证中完成此操作,因此与此类似:

 公共函数validate(){返回Validator :: make($ this->属性,['col1 | col2'=>'unique:mytable',//..])-> errors();} 

解决方案

如果要验证复合索引(多列)的唯一性,除非创建自定义验证规则,否则这是不可能的.

您可以创建自定义验证规则,请参见 https://laravel.com/docs/validation#custom-validation-rules

 //示例://'col1'=>'unique_with:table,col2,col3,col4等'//'col2'=>不需要再次检查唯一性,因为我们是针对col1进行的Validator :: extend('unique_with',function($ attribute,$ value,$ parameters,$ validator){$ request = request()-> all();//$ table始终是第一个参数//您可以将其扩展为使用点以指定:connection.database.table$ table = array_shift($ parameters);//将当前列添加到$ clauses数组$ clauses = [$ attribute =>$ value,];//添加其余的foreach($ parameters as $ column){如果(isset($ request [$ column])){$ clauses [$ column] = $ request [$ column];}}//查询是否存在.返回 !DB :: table($ table)->其中($ clauses)-> exists();}); 

将该代码放置在服务提供商的 boot()方法中,可以使用 App \ Http \ Providers \ AppServiceProvider.php 我没有进行测试,但是它应该可以帮助您继续进行必要的调整.

Is there a way to build a validation in Laravel which checks if two columns together are unique in a table?

So for example if there is [(A, B)] in my table, then the input (A, A) or (B, B) should pass the validation but (A, B) again have to fail.

For example like this:

DB::table('mytable')
            ->where([
                ['col1', $item->col1],
                ['col2', $item->col2]
            ])
            ->exists();

But I want to do it within a validation, so something simular to this:

public function validate() {
        return Validator::make($this->attributes, [
            'col1|col2' => 'unique:mytable',
            //....
        ])->errors();
    }

解决方案

If you are looking for validation of uniqueness of composite indexes (multiple columns), this is not possible unless you create a custom validation rule.

You can create a custom validation rule, see https://laravel.com/docs/validation#custom-validation-rules

// Example:
// 'col1' => 'unique_with:table,col2,col3,col4,etc'
// 'col2' => doesn't need to check uniqueness again, because we did it for col1

Validator::extend('unique_with', function ($attribute, $value, $parameters, $validator) {
    $request = request()->all();

  // $table is always the first parameter
  // You can extend it to use dots in order to specify: connection.database.table
  $table = array_shift($parameters);

  // Add current column to the $clauses array
  $clauses = [
    $attribute => $value,
  ];

  // Add the rest  
  foreach ($parameters as $column) {
    if (isset($request[$column])) {
        $clauses[$column] = $request[$column];
    }
  }

  // Query for existence.
  return ! DB::table($table)
            ->where($clauses)
            ->exists();
});

place that code in the boot() method of a service provider, you can use App\Http\Providers\AppServiceProvider.php I didn't test it, but it should help you to go forward and make the necessary adjustments.

这篇关于Laravel中用于连接两列的验证规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆