doucheng2210 2015-09-20 23: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 00: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'
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部