Laravel 4 - How to validate unique with multiple fields?

最后都变了- 提交于 2019-12-20 03:18:05

问题


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.. :)


回答1:


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

use it like this:

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



回答2:


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



来源:https://stackoverflow.com/questions/20674620/laravel-4-how-to-validate-unique-with-multiple-fields

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!