[英]Laravel form validation request unique ignorant doesn't work on update
我正在嘗試更新具有驗證所需的唯一名稱的數據。 但是,我無法讓它工作,因為它一直告訴我The name has already been taken.
. 已經試過了
請看一下我的驗證腳本:
<?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'
];
}
}
另外,我的控制器的更新腳本:
/**
* 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,
]);
}
我正在使用 Laravel 7.x,有什么想法嗎?
編輯
我想更新division_id
或description
字段。
編輯 2
我的表單是動態的,在 Vue 實例和Form
類中
data() {
return {
form: new Form({
division_id: "",
company_id: "",
name: "",
description: ""
}),
updateForm: new Form({
division_id: "",
company_id: "",
name: "",
description: ""
}),
filter: ""
};
},
嘗試這個,
'name' => "required|string|unique:departments,name,". $id,
這里 $id 表示您要更新的 id。 這將檢查所有名稱數據的唯一部分,但不檢查此 ID。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.