Laravel 4 - 如何使用具有软删除的唯一验证规则/唯一列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17458681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel 4 - how to use a unique validation rule / unique columns with soft deletes?
提问by Remluben
Assume, you are trying to create a new user, with a User model ( using soft deletes ) having a unique rule for it's email address, but there exists a trashed user within the database.
假设您正在尝试创建一个新用户,用户模型(使用软删除)对其电子邮件地址具有唯一规则,但数据库中存在一个垃圾用户。
When trying to validate the new user's data, you will get a validation error, because of the existing email.
尝试验证新用户的数据时,您将收到验证错误,因为存在现有电子邮件。
I made some kind of extra validation within my Controllers, but wouldn't it be nice to have it all within the Model?
我在我的控制器中进行了一些额外的验证,但是将所有这些都包含在模型中不是很好吗?
Would you suggest creating a custom validation rule?
您会建议创建自定义验证规则吗?
As I haven't found a clean solution now, I am interessted in how others solved this problem.
由于我现在还没有找到一个干净的解决方案,我对其他人如何解决这个问题很感兴趣。
回答by Gabriel Koerich
You can validate passing extra conditions:
您可以验证传递的额外条件:
'unique:users,deleted_at,NULL'
回答by petercoles
This sounds like an issue with your business logic rather than a technical problem.
这听起来像是您的业务逻辑问题,而不是技术问题。
The purpose of a soft delete is to allow for the possibility that the soft-deleted record may be restored in the future. However, if your application needs uniqueness of email (which is completely normal), you wouldn't want to both create a new user with that email address and be able to restore the old one as this would contravene the uniqueness requirement.
软删除的目的是允许将来恢复软删除的记录的可能性。但是,如果您的应用程序需要电子邮件的唯一性(这是完全正常的),您不希望使用该电子邮件地址创建新用户并能够恢复旧用户,因为这会违反唯一性要求。
So if there is a soft deleted record with the email address for which you are adding as a new record, you should consider instead restoring the original record and applying the new information to it as an update, rather than trying to circumvent the uniqueness check to create a new record.
因此,如果您添加的电子邮件地址有软删除记录作为新记录,您应该考虑恢复原始记录并将新信息应用到它作为更新,而不是试图规避唯一性检查创建一个新记录。
回答by Cesar Alonso
This is the best approach
这是最好的方法
'email' => 'required|email|unique:users,email,NULL,id,deleted_at,NULL',
It give you this query
它给你这个查询
select count(*) as aggregate from `users` where `email` = ? and `deleted_at` is null
回答by adrianthedev
Laravel offers "Additional Where Clauses".
Laravel 提供“附加 Where 子句”。
My url validation rule (from the updatemodel method) looks like this:
我的 url 验证规则(来自更新模型方法)如下所示:
$rules['url'] = 'required|unique:pages,url,'.$page->id.',id,deleted_at,NULL';
This means that the url must be unique, must ignore the current page and ignore the pages where deleted_at
id not NULL
.
这意味着 url 必须是唯一的,必须忽略当前页面并忽略deleted_at
id not的页面NULL
。
Hope this helps.
希望这可以帮助。
回答by Half Crazed
Your Eloquent model should have the $softDeletes
property set. If so, then when you perform a WHERE check, like User::where('username', 'jimbob')
, Eloquent will automatically add in the query WHERE deleted_at IS NULL
... which excludes soft deleted items.
你的 Eloquent 模型应该有$softDeletes
属性集。如果是这样,那么当您执行 WHERE 检查时,例如User::where('username', 'jimbob')
,Eloquent 将自动添加到查询中WHERE deleted_at IS NULL
... 排除软删除项目。