jeudi 19 décembre 2019

Laravel: Validation unique on update still failing all the time

I've an update form that contains an image and other data to be updated I changed the default route key to use the name instead of the default key which is the ID and I made a separate form request to validate my requests It works fine when posting new record unfortunately it keeps failing with the name field which is unique field; I've checked all threads on github and in stackoverflow with no use although I have the same project in laravel 5.5 and it works fine and now I'm stuck with laravel 6

hereis my form

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');
});

and here is my route

Route::resource('/dashboard/channels', 'ChannelController');

and here is my form request

<?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',
    ];
}
}

here is my controller for update method

public function update(Channel $channel, ChannelRequest $request)
{
    $channel->update([
        'name' => $request->name,
        'bg_base_color' => $request->base_color,
        'bg_complementary_color' => $request->complementary_color,
    ]);

    return response($channel->fresh(), 200);
}


via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire