doucheng2210 2015-09-21 07:54
浏览 38
已采纳

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

i have this table weapons that have the columns weaponID, weaponName, wStock, wDesc

in my update function inside the controller, i have this set of rules

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
}

when i updated and only changed the wStock it is having problems because it is saying that the weaponName already existed. so i used the unique rule

unique:weaponTbl,weaponName,' . $id,

because i wanted to except the record whose im currently working on but when i tested it i'am having these errors

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

why is that it using id only whereas i don't have any 'id' in my table but only weaponID? is there a way to work around with this?

  • 写回答

1条回答 默认 最新

  • dsq1982 2015-09-21 08:07
    关注

    Inside your weapon class you need to change the primary key:

    class Weapon extends Eloquent {
    
        protected $primaryKey = 'weaponID';
    
    }
    

    And then change the unique rule to:

    unique:weaponTbl,weaponName,' . $id . ',weaponID'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大