Laravel 独特的更新规则,无法正常工作
Laravel unique rules on update, not working properly
来自 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);
但是验证器说:
The department code has already been taken.
The name has already been taken.
怎么了?
我的 rules
数组是:
public static $rules = [
'name' => 'required|unique:departments|max:255',
'department_code' => 'required|unique:departments|max:127',
];
将您的 $rules
数组更改为:
public static $rules = [
'name' => 'required|max:255|unique:departments',
'department_code' => 'required|max:127|unique:departments',
];
然后你可以用它在规则中追加id
。
来自 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);
但是验证器说:
The department code has already been taken.
The name has already been taken.
怎么了?
我的 rules
数组是:
public static $rules = [
'name' => 'required|unique:departments|max:255',
'department_code' => 'required|unique:departments|max:127',
];
将您的 $rules
数组更改为:
public static $rules = [
'name' => 'required|max:255|unique:departments',
'department_code' => 'required|max:127|unique:departments',
];
然后你可以用它在规则中追加id
。