Laravel 尝试提供唯一字段验证时出现 sql 错误
Laravel Getting sql error when trying to give unique field validation
这是我用于标签表单验证的验证结果
public function rules()
{
return [
'name' => 'required|max:50|min:3|unique:tags,name,'.$this->tag,
];
}
我的控制器代码
public function update(TagValidation $request, Tag $tag )
{
$tag->update($request->all());
}
我试图在尝试更新时避免唯一文件验证问题。使用后
unique:tags,name,'.$this->tag
我低于 sql 错误
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'name:"abc"' in 'where clause' (SQL: select count(*) as aggregate from `tags` where `name` = abc and `name:"abc"` <> {"id":14 and `created_at:"2020-06-13T16:24:36`.`000000Z"` = updated_at:"2020-06-13T16:46:44.000000Z"})
但是我在数据库中有名称列,如果我在验证中不使用 $this->tag,存储工作正常。
请注意,因为唯一验证是
唯一:table,列,除,idColumn
您正在传递标签的值 anme 但不是必需的
您实际使用:
return [
'name' => 'required|max:50|min:3|unique:tags,name,'.$this->tag,
];
但你需要使用这个我向你展示了在存储和更新时使用相同验证的工作示例(POST 和 PUT 方法):
public function rules()
{
if ($this->method() == 'PUT')
{
return [
'name' => 'required|unique:tags,name,'.$this->id.',id',
];
}elseif ($this->method() == 'POST'){
return [
'name' => 'required|unique:tags,name'
];
}
}
包含在Laravel 7*可以直接使用Model
public function rules()
{
// Check Create or Update
if ($this->method() == 'PUT')
{
return [
'name' => 'required|unique:App\Tag,name,'.$this->id.',id'
];
}elseif ($this->method() == 'POST'){
return [
'name' => 'required|unique:App\Tag,name'
];
}
}
您应该传递要忽略的唯一规则的记录的 ID,我假设该规则是标签:
'name' => 'required|max:50|min:3|unique:tags,name,'. $this->tag->id,
或者您可以使用规则的对象版本,您可以直接将模型传递给:
'name' => [
'required', 'max:50', 'min:3',
Rule::unique('tags')->ignore($this->tag),
],
这是我用于标签表单验证的验证结果
public function rules()
{
return [
'name' => 'required|max:50|min:3|unique:tags,name,'.$this->tag,
];
}
我的控制器代码
public function update(TagValidation $request, Tag $tag )
{
$tag->update($request->all());
}
我试图在尝试更新时避免唯一文件验证问题。使用后
unique:tags,name,'.$this->tag
我低于 sql 错误
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'name:"abc"' in 'where clause' (SQL: select count(*) as aggregate from `tags` where `name` = abc and `name:"abc"` <> {"id":14 and `created_at:"2020-06-13T16:24:36`.`000000Z"` = updated_at:"2020-06-13T16:46:44.000000Z"})
但是我在数据库中有名称列,如果我在验证中不使用 $this->tag,存储工作正常。
请注意,因为唯一验证是
唯一:table,列,除,idColumn
您正在传递标签的值 anme 但不是必需的
您实际使用:
return [
'name' => 'required|max:50|min:3|unique:tags,name,'.$this->tag,
];
但你需要使用这个我向你展示了在存储和更新时使用相同验证的工作示例(POST 和 PUT 方法):
public function rules()
{
if ($this->method() == 'PUT')
{
return [
'name' => 'required|unique:tags,name,'.$this->id.',id',
];
}elseif ($this->method() == 'POST'){
return [
'name' => 'required|unique:tags,name'
];
}
}
包含在Laravel 7*可以直接使用Model
public function rules()
{
// Check Create or Update
if ($this->method() == 'PUT')
{
return [
'name' => 'required|unique:App\Tag,name,'.$this->id.',id'
];
}elseif ($this->method() == 'POST'){
return [
'name' => 'required|unique:App\Tag,name'
];
}
}
您应该传递要忽略的唯一规则的记录的 ID,我假设该规则是标签:
'name' => 'required|max:50|min:3|unique:tags,name,'. $this->tag->id,
或者您可以使用规则的对象版本,您可以直接将模型传递给:
'name' => [
'required', 'max:50', 'min:3',
Rule::unique('tags')->ignore($this->tag),
],