diff --git a/src/Illuminate/Validation/Rules/Unique.php b/src/Illuminate/Validation/Rules/Unique.php index 519b66c5d5a..0738d83d122 100644 --- a/src/Illuminate/Validation/Rules/Unique.php +++ b/src/Illuminate/Validation/Rules/Unique.php @@ -73,4 +73,23 @@ public function __toString() $this->formatWheres() ), ','); } + + /** + * Apply a condition to exclude soft-deleted records. + * + * @param Model|null $model + * @return $this + */ + public function withSoftDeletes(?Model $model = null): static + { + $this->where(function ($query) use ($model) { + $deletedAtColumn = $model && method_exists($model, 'getDeletedAtColumn') + ? $model->getDeletedAtColumn() + : 'deleted_at'; + + $query->whereNull($deletedAtColumn); + }); + + return $this; + } }