Skip to content

Commit

Permalink
feat: support customizable soft delete column in unique validation
Browse files Browse the repository at this point in the history
Ensure the soft delete column can be dynamically determined based on the model.
  • Loading branch information
mohammadrasoulasghari committed Jan 20, 2025
1 parent d887b41 commit 3be1960
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/Illuminate/Validation/Rules/Unique.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 3be1960

Please sign in to comment.