3
我有一个CategoryRequest.php文件和一个唯一的字段'name'验证。 当我使用表单创建时,它可以工作,因此它可以防止插入具有相同名称的类别。忽略更新时的唯一验证ID
问题是,当试图更新它,说:'名称已被采取。'或者如果我尝试下面的代码,它会说:'Undefined Variable:id'。
如何更新,验证时忽略自己的名字? 这里是我的代码:
<?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'
];
}
}