Handle unique validation conflicts caused by soft-deleted records#54275
Closed
mohammadrasoulasghari wants to merge 2 commits intolaravel:11.xfrom
Closed
Handle unique validation conflicts caused by soft-deleted records#54275mohammadrasoulasghari wants to merge 2 commits intolaravel:11.xfrom
mohammadrasoulasghari wants to merge 2 commits intolaravel:11.xfrom
Conversation
Allows excluding soft-deleted records during unique validation.
| public function withSoftDeletes() | ||
| { | ||
| $this->where(function ($query) { | ||
| $query->whereNull('deleted_at'); |
Contributor
There was a problem hiding this comment.
This field may be named differently in each model.
6c8d962 to
358a8c2
Compare
Ensure the soft delete column can be dynamically determined based on the model.
358a8c2 to
3be1960
Compare
Contributor
|
Doesn't framework/src/Illuminate/Validation/Rules/DatabaseRule.php Lines 179 to 190 in 122e5c2 |
shaedrich
reviewed
Jan 20, 2025
Comment on lines
+85
to
93
| $this->where(function ($query) use ($model) { | ||
| $deletedAtColumn = $model && method_exists($model, 'getDeletedAtColumn') | ||
| ? $model->getDeletedAtColumn() | ||
| : 'deleted_at'; | ||
|
|
||
| $query->whereNull($deletedAtColumn); | ||
| }); | ||
|
|
||
| return $this; |
Contributor
There was a problem hiding this comment.
You could use tap() here:
Suggested change
| $this->where(function ($query) use ($model) { | |
| $deletedAtColumn = $model && method_exists($model, 'getDeletedAtColumn') | |
| ? $model->getDeletedAtColumn() | |
| : 'deleted_at'; | |
| $query->whereNull($deletedAtColumn); | |
| }); | |
| return $this; | |
| return tap($this)->where(function ($query) use ($model) { | |
| $deletedAtColumn = $model && method_exists($model, 'getDeletedAtColumn') | |
| ? $model->getDeletedAtColumn() | |
| : 'deleted_at'; | |
| $query->whereNull($deletedAtColumn); | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Hi Laravel Team 👋,
During my experience working on various projects, I’ve encountered a recurring issue when validating the uniqueness of a field while using Soft Deletes. Specifically, when using Rule::unique for validation, it often leads to unintended conflicts because soft-deleted records are still considered in the validation check. This has caused confusion and bugs in projects where certain records are logically “deleted” but still exist in the database.
To address this issue, I’ve introduced a new method called withSoftDeletes to the Rule::unique class. This method allows developers to exclude soft-deleted records from the unique validation check. By explicitly opting in to this behavior, it ensures that the framework remains opinionated and avoids introducing any breaking changes.
I believe this addition can improve the developer experience by:
Usage Example:
Here’s an example of how this method can be used:
Thank you for considering this PR! 😊
Looking forward to your feedback and suggestions.