From d887b41ef84ae8365f8cdcbf7e6dea8c1750740c Mon Sep 17 00:00:00 2001 From: mohammadrasoulasghari Date: Mon, 20 Jan 2025 21:37:47 +0330 Subject: [PATCH 1/2] feat(Validation): add withSoftDeletes method to Unique rule Allows excluding soft-deleted records during unique validation. --- src/Illuminate/Validation/Rules/Unique.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Illuminate/Validation/Rules/Unique.php b/src/Illuminate/Validation/Rules/Unique.php index 519b66c5d5aa..48213934e5fc 100644 --- a/src/Illuminate/Validation/Rules/Unique.php +++ b/src/Illuminate/Validation/Rules/Unique.php @@ -73,4 +73,18 @@ public function __toString() $this->formatWheres() ), ','); } + + /** + * Exclude soft-deleted records from the unique check. + * + * @return $this + */ + public function withSoftDeletes() + { + $this->where(function ($query) { + $query->whereNull('deleted_at'); + }); + + return $this; + } } From 3be19600c278bd69014891df8a5553e335549274 Mon Sep 17 00:00:00 2001 From: mohammadrasoulasghari Date: Mon, 20 Jan 2025 22:42:08 +0330 Subject: [PATCH 2/2] feat: support customizable soft delete column in unique validation Ensure the soft delete column can be dynamically determined based on the model. --- src/Illuminate/Validation/Rules/Unique.php | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/Illuminate/Validation/Rules/Unique.php b/src/Illuminate/Validation/Rules/Unique.php index 48213934e5fc..0738d83d122c 100644 --- a/src/Illuminate/Validation/Rules/Unique.php +++ b/src/Illuminate/Validation/Rules/Unique.php @@ -75,14 +75,19 @@ public function __toString() } /** - * Exclude soft-deleted records from the unique check. + * Apply a condition to exclude soft-deleted records. * + * @param Model|null $model * @return $this */ - public function withSoftDeletes() + public function withSoftDeletes(?Model $model = null): static { - $this->where(function ($query) { - $query->whereNull('deleted_at'); + $this->where(function ($query) use ($model) { + $deletedAtColumn = $model && method_exists($model, 'getDeletedAtColumn') + ? $model->getDeletedAtColumn() + : 'deleted_at'; + + $query->whereNull($deletedAtColumn); }); return $this;