【发布时间】:2017-10-05 06:57:01
【问题描述】:
我有一个 CategoryRequest.php 文件和字段“名称”的唯一验证。 当我使用表单创建时,它可以工作,因此它可以防止插入具有相同名称的类别。
问题是当尝试更新它时,会说:“该名称已被占用。”或者如果我尝试下面的代码,它会显示:'未定义变量: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'
];
}
}
【问题讨论】: