【发布时间】:2020-10-03 09:54:25
【问题描述】:
这是来自我用于标签表单验证的验证
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"})
但我在数据库中有 name 列,如果我在验证中不使用 $this->tag,存储工作正常。
【问题讨论】:
-
我假设您想在使用
$this->tag时将某个字段的值传递给规则(可能是模型的 id),而不是模型实例本身? -
如果我使用 $this->id ,我会收到“名称已被占用。”
-
$this->tag是一个模型实例...您正在将模型实例连接到您正在构建的字符串(序列化模型),而不是像那里的 id 那样传递单个值... @ 987654328@ 将是标签的 id -
使用 $this->tag->id 后,我收到验证错误“名称已被占用。”看到这个 ans stackoverflow.com/questions/23587833/… 后,我使用了模型实例。
-
我的路线是 tags/14/edit
标签: laravel laravel-7.x