Laravel 4 - 如何验证多个字段的唯一性?

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

Laravel 4 - How to validate unique with multiple fields?

phpvalidationlaravellaravel-4schema

提问by Melvin

I have this schema:

我有这个架构:

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

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

My problem is, how to validate these unique fields?

我的问题是,如何验证这些独特的字段?

I tried this but I know this is wrong...

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

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

Any help is appreciated.. :)

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

回答by rahmat

You should use this package https://github.com/felixkiss/uniquewith-validator

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

use it like this:

像这样使用它:

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

回答by Anam

Try the following:

请尝试以下操作:

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


 'lname' => 'unique:members,lname',
                            ^^^^^ "lname" is a column of members table

More info:

更多信息:

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

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