Laravel 4 - how to use a unique validation rule / unique columns with soft deletes? Laravel 4 - how to use a unique validation rule / unique columns with soft deletes? laravel laravel

Laravel 4 - how to use a unique validation rule / unique columns with soft deletes?


You can validate passing extra conditions:

'unique:users,deleted_at,NULL'


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.


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