Laravel 4-如何使用唯一验证规则/唯一列进行软删除? [英] Laravel 4 - how to use a unique validation rule / unique columns with soft deletes?
问题描述
假设,您正在尝试创建一个新用户,其用户模型(使用软删除)对其电子邮件地址具有唯一的规则,但是数据库中存在一个已删除用户.
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.
我在Controller中进行了一些额外的验证,但是将其全部包含在模型中会不是很好吗?
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.
推荐答案
这听起来像是您的业务逻辑问题,而不是技术问题.
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.
这篇关于Laravel 4-如何使用唯一验证规则/唯一列进行软删除?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!