Php Laravel 4-未找到验证程序列错误
尝试使用唯一验证规则时,我收到以下错误:Php Laravel 4-未找到验证程序列错误,php,laravel,laravel-4,validation,Php,Laravel,Laravel 4,Validation,尝试使用唯一验证规则时,我收到以下错误: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: select count(*) as aggregate from `empleados` where `cedula` = asd and `id` <> asd) 这是我的模型: <?php class Empleado extends Eloquent {
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'id' in 'where clause' (SQL: select count(*) as aggregate from `empleados` where `cedula` = asd and `id` <> asd)
这是我的模型:
<?php
class Empleado extends Eloquent {
protected $table = 'empleados';
protected $primaryKey = 'cedula';
public $errors;
protected $fillable = array('cedula','nombre','apellido','sexo');
public function isValid($data,$edit)
{
$rules = array(
'cedula' => 'required|unique:empleados',
'nombre' => 'required|max:40',
'apellido' => 'required|max:40',
'sexo' => 'required'
);
if($edit){
$rules['cedula'] .= ',cedula,' . $this->getKey();
}
(...)
您是否尝试过:
// Set Primary to null
protected $primaryKey = null;
// Set increment to false
public $incrementing = false;
只需更改以下内容:
'cedula' => 'required|unique:empleados',
致:
稍后,在编辑时,只需添加要忽略的值
if($edit){
$rules['cedula'] .= ',' . $this->getKey() . ',cedula';
}
那是因为拉威尔做到了这一点:
检查值foo
对于列cedula
但忽略具有值条的行
现在,如果您没有指定应该根据条检查哪个列,那么Laravel将默认使用id
。要覆盖它,只需将该列作为unique
的第四个参数传递,该参数与问题无关,但与$table->create()有关代码>?Ups。我没有注意到。这张桌子是一位朋友做的。已经修好了。谢谢。主键不应该是可填充的@啊,你说得对。这是一个设计错误。你这是什么意思?我按照你的建议做了,但还是出现了错误,伙计,这很有效。非常感谢=)现在,你知道为什么会发生这种情况吗?在答案中添加了解释。此外,在您的情况下,第1部分不是必需的,如果钥匙不是cedula
(col name),则需要第1部分。谢谢您的解释,我想是这样的。(Cédula是我们在委内瑞拉给身份证件起的名字)。我在哪里加上这个名字?在我的模型中还是在elount/model.php中?
'cedula' => 'required|unique:empleados,cedula',
if($edit){
$rules['cedula'] .= ',' . $this->getKey() . ',cedula';
}