Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Add ignoreTrashed to Unique validation rule #38124

Merged
merged 3 commits into from
Jul 26, 2021
Merged

[8.x] Add ignoreTrashed to Unique validation rule #38124

merged 3 commits into from
Jul 26, 2021

Conversation

PhiloNL
Copy link
Contributor

@PhiloNL PhiloNL commented Jul 24, 2021

Hi all!

I find myself adding whereNull('deleted_at') to the Rule::unique() quite often when working with models which can be soft deleted. This PR will add a simple helper to the unique validation rule.

Have a great day! 😄

// Before
[
  'email'=> [
    Rule::unique('users')->whereNull('deleted_at'),
  ],
];

// After
[
  'email'=> [
    Rule::unique('users')->ignoreTrashed(),
  ],
];

@taylorotwell taylorotwell merged commit 0005bdc into laravel:8.x Jul 26, 2021
@PhiloNL PhiloNL deleted the feature/validation-unique-ignore-trashed branch July 27, 2021 08:08
@rakibdevs
Copy link

What happens while restoring temporary deleted records? It could have possible duplicate entries.

@PhiloNL
Copy link
Contributor Author

PhiloNL commented Jul 29, 2021

@rakibdevs this very much depends on your specific use case for using soft deletes. For example, I often use soft-deleted as a temporary backup. I prune all soft deleted models after a couple of days. So restoring models doesn't happen very often.

Alternatively, if you, for example, allow your users to restore data, I would implement a check to verify if the record is still unique. If the record is no longer unique, I will prompt the user to, e.g., change their username before restoring.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants