[英]how can i solve the unique field error in laravel?
'call_signs' => [
'required', 'array', 'min:1',
'call_signs.*.call_sign' => [
Rule::unique('call_signs', 'call_sign')->whereNull('deleted_at')->ignore($user->id, 'user_id')
]
],
this is my unique code it gives me the error这是我的唯一代码,它给了我错误
call_signs is an array call_signs 是一个数组
BadMethodCallException Method Illuminate\Validation\Validator::validateUnique:callSigns,callSign,"12",userId,deletedAt,"NULL" does not exist.
BadMethodCallException 方法 Illuminate\Validation\Validator::validateUnique:callSigns,callSign,"12",userId,deletedAt,"NULL" 不存在。
my Callsign table has softDelete我的 Callsign 表有 softDelete
Laravel's Unique Validation Rule provides only ignore
method and not whereNull
method. Laravel 的唯一验证规则仅提供
ignore
方法,不提供whereNull
方法。 Please note that whereNull
method is part of Database Query Builder.请注意
whereNull
方法是 Database Query Builder 的一部分。
Also, it is usually not good to check for uniqueness on soft deletes on tables.此外,检查表上软删除的唯一性通常也不好。 You should consider using hard delete for such use case.
您应该考虑对此类用例使用硬删除。
Anyway, if you want to use for some reason, you can create your own unique custom validation rule.无论如何,如果您出于某种原因想要使用,您可以创建自己独特的自定义验证规则。 You can read more about ithere , contains example also.
您可以在此处阅读更多相关信息,也包含示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.