【问题标题】:Laravel 4 - How to validate unique with multiple fields?Laravel 4 - 如何验证多个字段的唯一性?
【发布时间】:2018-04-28 18:07:29
【问题描述】:

我有这个架构:

Schema::create('members', function(Blueprint $table)
    {
        $table->increments('id');
        $table->string('lname');
        $table->string('fname');
        $table->integer('mname');

        $table->unique(array('lname', 'fname'));
    });

我的问题是,如何验证这些唯一字段?

我试过了,但我知道这是错误的......

public static $rules = array(
    'lname' => 'unique:members',
    'fname' => 'unique:members'
);

任何帮助表示赞赏.. :)

【问题讨论】:

    标签: php validation laravel laravel-4 schema


    【解决方案1】:

    你应该使用这个包https://github.com/felixkiss/uniquewith-validator

    像这样使用它:

    $rules = array(
        '<field1>' => 'unique_with:<table>,<field2>[,<field3>,...,<ignore_rowid>]',
    );
    

    【讨论】:

      【解决方案2】:

      尝试以下方法:

      public static $rules = array(
          'lname' => 'unique:members,lname',
          'fname' => 'unique:members,fname'
      );
      
      
       'lname' => 'unique:members,lname',
                                  ^^^^^ "lname" is a column of members table
      

      更多信息:

      http://laravel.com/docs/validation#rule-unique

      【讨论】:

      • 我认为这将验证每个 lnamefname 是否是唯一的.. 我需要验证两者是否是唯一的组合..
      • 在这种情况下,您需要自定义验证。
      猜你喜欢
      • 2023-03-20
      • 2014-08-08
      • 2019-03-24
      • 2010-12-10
      • 2013-08-30
      • 2019-01-04
      • 1970-01-01
      • 2023-04-09
      相关资源
      最近更新 更多