[英]Laravel form validation request unique ignorant doesn't work on update
I'm trying to update a data which has a unique name required on validation.我正在尝试更新具有验证所需的唯一名称的数据。 However, I can't get it to work because it keep telling me that
The name has already been taken.
但是,我无法让它工作,因为它一直告诉我
The name has already been taken.
. . Already tried the
已经试过了
Please have a look at my validation script:请看一下我的验证脚本:
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class RequestDepartment extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'division_id' => 'required|numeric',
'name' => "sometimes|required|string|unique:departments,name,{$this->id},id",
'description' => 'sometimes|nullable|string'
];
}
}
also, my controller's update script:另外,我的控制器的更新脚本:
/**
* Update the specified resource in storage.
*
* @param \App\Http\Requests\RequestDepartment $request
* @param \App\Department $department
* @return \Illuminate\Http\Response
*/
public function update(RequestDepartment $request, Department $department)
{
$department->update($request->validated());
$department = Department::whereId($department->id)->with('division')->first();
return response()->json([
'updated' => true,
'data' => $department,
]);
}
I'm using Laravel 7.x, any idea please?我正在使用 Laravel 7.x,有什么想法吗?
EDIT编辑
I want to update the division_id
or description
field.我想更新
division_id
或description
字段。
EDIT 2编辑 2
My form is dynamic, inside Vue instance and Form
class我的表单是动态的,在 Vue 实例和
Form
类中
data() {
return {
form: new Form({
division_id: "",
company_id: "",
name: "",
description: ""
}),
updateForm: new Form({
division_id: "",
company_id: "",
name: "",
description: ""
}),
filter: ""
};
},
Try This,尝试这个,
'name' => "required|string|unique:departments,name,". $id,
Here $id means wchich id you want to update.这里 $id 表示您要更新的 id。 this will check unique portion all name data but not for this id.
这将检查所有名称数据的唯一部分,但不检查此 ID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.