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.
我在我的控制器中进行了一些额外的验证,但是将其全部包含在模型中不是很好吗?
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屋!