Php Laravel:更新时的唯一验证始终失败

Php Laravel:更新时的唯一验证始终失败,php,laravel,vue.js,laravel-5,Php,Laravel,Vue.js,Laravel 5,我有一个更新表单,其中包含要更新的图像和其他数据我更改了默认路由键以使用名称,而不是默认键(即ID),并且我提出了一个单独的表单请求以验证我的请求。发布新记录时,该表单工作正常。不幸的是,该表单始终无法通过唯一的name字段领域我已经检查了github和stackoverflow中的所有线程,但没有任何用处,尽管我在Laravel5.5中有相同的项目,它工作正常,现在我仍然坚持使用Laravel6 这是我的表格 let data = new FormData(); data.append('na

我有一个更新表单,其中包含要更新的图像和其他数据我更改了默认路由键以使用名称,而不是默认键(即ID),并且我提出了一个单独的表单请求以验证我的请求。发布新记录时,该表单工作正常。不幸的是,该表单始终无法通过唯一的name字段领域我已经检查了github和stackoverflow中的所有线程,但没有任何用处,尽管我在Laravel5.5中有相同的项目,它工作正常,现在我仍然坚持使用Laravel6

这是我的表格

let data = new FormData();
data.append('name', this.channel.name);
data.append('base_color', this.channel.baseColor);
data.append('complementary_color', this.channel.complementaryColor);
if (this.file){
  data.append('avatar', this.file);
}

data.append('_method', 'PUT');

axios.post(`/dashboard/channels/${this.channel.name}`, data).then(resp => {
  this.$parent.$emit('channel_updated', resp.data);
}).catch(error => {
  flash(error.response.data, 'danger', 'backEndStyle');
});
这是我的路线

Route::resource('/dashboard/channels', 'ChannelController');
这是我的申请表

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class ChannelRequest 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 [
        'name' => 'required|unique:channels,name,'. $this->id,
        'base_color' => 'required',
        'complementary_color' => 'required',
    ];
}
}

验证唯一性时,使用
忽略
约束忽略当前模型

public function rules()
{
    return [
        'name' => ['required', Rule::unique('channels')->ignore($this->route('channel'))],
        'base_color' => 'required',
        'complementary_color' => 'required',
    ];
}

如果路由参数名为
channel
,为什么要使用
id
?它将基于“id”列忽略,除非另有说明,即使在我使用“name”=>“required | unique:channels,name”时也是如此$此->名称,或“名称”=>“必需|唯一:频道,名称,”$此->频道->名称,仍然失败您的意思是
$this->route('channel')->id
?正如模型中的“id”一样,因为这是规则设置要忽略的,id字段,如果没有其他说明的话?如果您觉得可以改进答案,则始终有一个编辑按钮。我不改变人们的意图,我只希望有对OP有效的答案
public function rules()
{
    return [
        'name' => ['required', Rule::unique('channels')->ignore($this->route('channel'))],
        'base_color' => 'required',
        'complementary_color' => 'required',
    ];
}