【问题标题】:Laravel form validation rules for checking unique using primary key and foreign key使用主键和外键检查唯一性的 Laravel 表单验证规则
【发布时间】:2015-04-18 00:03:50
【问题描述】:

我知道如何使用单个 id 来检查唯一性,如下所示,

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

然后我想用检查唯一的两个id(主键和外键)

这是我的代码:

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

但我有以下错误:

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)

我该如何解决这个问题?

【问题讨论】:

标签: php laravel laravel-4 eloquent laravel-validation


【解决方案1】:

你传递了错误的参数,后面跟着 doc

unique:table,column,except,idColumn

You may also specify more conditions that will be added as "where" clauses to the query:

'email' => 'unique:users,email_address,NULL,id,account_id,1'
In the rule above, only rows with an account_id of 1 would be included in the unique check.

因此,您可以按自己的方式传递外键。

【讨论】:

  • 我有这个错误 "SQLSTATE[42S22]: Column not found: 1054 Unknown column '3' in 'where clause' (SQL: select count(*) as aggregate from class where @987654323 @ = g1 和 3 schoolid 和 20 = AutoID)"
  • 对不起,您还应该使用第二个参数更改顺序。更改 $data 和 AutoID。我编辑了帖子
  • 我有这个错误 "SQLSTATE[42S22]: Column not found: 1054 Unknown column '3' in 'where clause' (SQL: select count(*) as aggregate from class where @987654327 @ = g1 和 3 schoolid 和 AutoID = 20)"
【解决方案2】:

我已使用以下代码修复此错误

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-02-27
    • 2019-11-15
    • 1970-01-01
    • 2011-06-18
    • 2018-05-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多