2015-09-21 105 views
2

我必须有列此表的武器weaponID,weaponName,wStock,wDesclaravel 4的独特规则对更新数据库记录

在控制器内我更新的功能,我有这样的一套规则

public function c_updateSystemUser($id) 
{ 
    $rules = array(
      'weaponName' => 'required|min:2|max:50|regex:/^[a-zA-Z0-9\-\s]+$/|unique:weaponTbl,weaponName,' . $id, 
      'wStock'  => 'required|numeric', 
      'wDesc'  => 'required|min:1|max:100|regex:/^[a-zA-Z0-9\-\s]+$/' 

     ); 
    //other stuff 
} 

当我更新,只改变了wStock它有问题,因为它说武器名已经存在。所以我使用的唯一规则

unique:weaponTbl,weaponName,' . $id, 

,因为我想除因即时通讯目前正在使用的记录,但是当我测试过我'有这些错误

Column not found: 1054 Unknown column 'id' in 'where clause' 
(SQL: select count(*) as aggregate from `weaponTbl` where `weaponName` = unicorn and `id` <> 1) 

为什么只有使用它的id而我的表中没有任何'id',但只有weaponID?有没有办法解决这个问题?

回答

1

里面你的武器类,你需要更改主键:

class Weapon extends Eloquent { 

    protected $primaryKey = 'weaponID'; 

} 

,然后改变其独特的规则:

unique:weaponTbl,weaponName,' . $id . ',weaponID' 
+0

仍然有同样的错误,这里是我的武器模型类 '<?php use Illuminate \ Auth \ UserTrait; 使用Illuminate \ Auth \ UserInterface; 使用Illuminate \ Auth \ Reminders \ RemindableTrait; 使用Illuminate \ Auth \ Reminders \ RemindableInterface; class Weapon extends Eloquent implements UserInterface,RemindableInterface { \t use UserTrait,RemindableTrait; \t public $ timestamps = false; \t protected $ primaryKey ='weaponID'; \t protected $ table ='weaponTbl; \t protected $ hidden = array('password','remember_token'); } ' – devOG

+1

嗯......尝试将唯一规则更改为'unique:weaponTbl,weaponName'。 $ id。 ',weaponID'' –

+0

谢谢!有效!非常感谢! :) – devOG