【问题标题】:using unique rule of laravel 4 on updating database records使用 laravel 4 的独特规则更新数据库记录
【发布时间】:2015-12-17 20:19:26
【问题描述】:

我有这个表武器,其中包含武器 ID、武器名称、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 时,它遇到了问题,因为它说武器名称已经存在。所以我使用了独特的规则

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”而只有武器 ID?有没有办法解决这个问题?

【问题讨论】:

    标签: php laravel laravel-4 laravel-validation


    【解决方案1】:

    在您的武器类中,您需要更改主键:

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

    然后将唯一规则改为:

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

    【讨论】:

    • 仍然有同样的错误这里是我的武器模型类&lt;?php use Illuminate\Auth\UserTrait; use Illuminate\Auth\UserInterface; use Illuminate\Auth\Reminders\RemindableTrait; use Illuminate\Auth\Reminders\RemindableInterface; class Weapon extends Eloquent implements UserInterface, RemindableInterface { use UserTrait, RemindableTrait; public $timestamps = false; protected $primaryKey = 'weaponID'; protected $table = 'weaponTbl; protected $hidden = array('password', 'remember_token'); }
    • 嗯...尝试将唯一规则更改为unique:weaponTbl,weaponName,' . $id . ',weaponID'
    • 没问题,我会编辑答案以包含找到的解决方案。
    猜你喜欢
    • 2014-11-29
    • 1970-01-01
    • 2017-04-28
    • 1970-01-01
    • 2016-09-27
    • 1970-01-01
    • 1970-01-01
    • 2019-06-24
    • 1970-01-01
    相关资源
    最近更新 更多