简体   繁体   English

如何解决 laravel 中的唯一字段错误?

[英]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.

相关问题 如何解决 laravel php artisan migrate 错误问题? - how can I solve laravel php artisan migrate error problem? 如何解决 web laravel 中的混合内容错误? - How can I solve the mixed content error in web laravel? 如何解决我的 Laravel model 中的此错误? - How can I solve this error in my Laravel model? 当我尝试更新唯一字段时出现 laravel 错误 - laravel error when i try to update unique field 如何在 laravel 中生成唯一 ID? - How I can generate the unique ID in laravel? Laravel 8 - 我收到此错误“SQLSTATE [42S02]”我该如何解决? - Laravel 8 - I got this error 'SQLSTATE[42S02]' how can I solve this? 如何解决我在创建新的 Laravel 7 项目后遇到的 npm 运行开发错误? - How can I solve the npm run dev error I'm facing after creating a new Laravel 7 project? 未定义的属性:> 在 Laravel 上使用日期类型列时出现 stdClass 错误。 我该如何解决? - Undefined property: > stdClass error when using date type column on Laravel. How can I solve it? 在 DB Seeding Laravel Roles 期间我如何解决此错误 - How i can solve this error while during DB Seeding Laravel Roles 我该如何解决 [Vue 警告]:数据()中的错误:Laravel Vue 中的“ReferenceError:total is not defined” - How can i solve [Vue warn]: Error in data(): “ReferenceError: total is not defined” in Laravel Vue
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM