Melvin
Melvin

Reputation: 6008

Laravel 4 - How to validate unique with multiple fields?

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

Upvotes: 2

Views: 6771

Answers (2)

rahmat
rahmat

Reputation: 1757

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>]',
);

Upvotes: 6

Anam
Anam

Reputation: 12199

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

Upvotes: 3

Related Questions