使用 laravel 4 的唯一规则更新数据库记录

using unique rule of laravel 4 on updating database records

我有这个 table 武器,其列为 weaponID、weaponName、wStock、wDesc

在我控制器内的更新函数中,我有这组规则

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 时出现问题,因为它说 weaponName 已经存在。所以我使用了独特的规则

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 而我的 table 中没有任何 'id' 而只有 weaponID?有办法解决这个问题吗?

在你的武器里面class你需要改变主键:

class Weapon extends Eloquent {

    protected $primaryKey = 'weaponID';

}

然后将唯一规则更改为:

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