Ignore unique check id on update

I have a CategoryRequest.php file and a unique validation for the field name. When I use the form to create it works, so it doesn't allow inserting a category with the same name.

The problem is when you try to update it, it says "This name has already been taken." or if i try the code below it says 'Undefined Variable: id'.

How can I update and ignore my name on checkout? Here is my code:

<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class CategoryRequest 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:categories,name,'.$id.',id'
        ];
    }
}

      

+3


source to share


1 answer


change this:

return [
    'name' => 'required|unique:categories,name,'.$id.',id'
];

      

:



return [
    'name' => 'required|' . Rule::unique('categories')->ignore('category')
];

      

or that:

return [
    'name' => ['required', Rule::unique('categories')->ignore('category')]
];

      

+2


source







All Articles