Q:

Ignore unique validation id on update

I have a CategoryRequest.php file and a unique validation for field 'name'.
When I use the form to create, it works, so it prevents from inserting a category with the same name.
The problem is when try to update it, is says: 'The name has already been taken.' or if I try the code below, it says: 'Undefined Variable: id'.
How can I update and it ignores its own name when validating?
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'
        ];
    }
}

A:

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

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

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

