【问题标题】:Laravel unique rules on update, not working properlyLaravel 独特的更新规则,无法正常工作
【发布时间】:2017-04-28 09:41:26
【问题描述】:

来自This 的回答我正在尝试更新部门数据。代码如下:

$id = Crypt::decrypt($id); 
        $rules = Department::$rules;

        $rules['name']              = $rules['name'] . ',id,' . $id;
        $rules['department_code']   = $rules['department_code'] . ',id,' . $id;

        dump($rules);

        $validator = Validator::make($data = $request->all(), $rules);
        if ($validator->fails()) return Redirect::back()->withErrors($validator)->withInput();

        $department = Department::findOrFail($id);

但是验证器说:

部门代码已被占用。

这个名字已经被占用了。

那怎么了?

我的rules 数组是:

public static $rules = [
        'name'              =>  'required|unique:departments|max:255', 
        'department_code'   =>  'required|unique:departments|max:127',
    ];

【问题讨论】:

    标签: laravel-5 unique updates rules


    【解决方案1】:

    将您的 $rules 数组更改为:

    public static $rules = [
            'name'              =>  'required|max:255|unique:departments', 
            'department_code'   =>  'required|max:127|unique:departments',
        ];
    

    然后您可以使用它在规则中附加id

    【讨论】:

    • 我已将规则更改为 public static $rules = [ 'name' => 'required|max:255|unique:departments', 'department_code' => 'required|max:127|unique:departments', ]; 。现在name 规则工作正常,但仍然收到消息The department code has already been taken.
    • 表中department_code 的列名称是什么?如果不是department_code,那么您必须在规则中提供unique:departments,codeDocs
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-24
    • 1970-01-01
    • 2016-03-03
    • 2017-06-17
    相关资源
    最近更新 更多