2

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

2 Answers 2

6

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>]',
);
Sign up to request clarification or add additional context in comments.

Comments

3

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

2 Comments

I think this would validate if each lname and fname is unique.. What I need to to validate both as unique combined..
In that case, you need a custom validation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.