Search code examples
phplaravellaravel-4eloquentlaravel-validation

Laravel form validation rules for checking unique using primary key and foreign key


I know how to check unique using single id like following,

$addrules = array(
        'GradeName' =>  array('required','regex:/^./','unique:class,GradeName,'.$id.',AutoID'),   
                             );

then i want to use to check unique two id (primary and foreign key)

This is my code:

$update = array(
        'GradeName' =>  array('required','regex:/^./','unique:class,GradeName,'.$schoolid.',schoolid,'.$data.',AutoID'),   
                             );

But i have following error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column '20' in 'where clause' (SQL: select count(*) as aggregate from `class` where `GradeName` = g1 and `schoolid` <> 3 and `20` = AutoID)

How can i solve this?


Solution

  • I have fixed this error using following code

    $update = array(
            'GradeName' =>  array('required','regex:/^./','unique:class,GradeName,'.$data.',AutoID,schoolid,'.$schoolid),   
                                 );