Skip to content

Conversation

yaminlam
Copy link

This PR adds a new custom validation rule UniqueWithTrashed that allows developers to enforce uniqueness of a column while ignoring soft-deleted records (deleted_at is not null).

Why it’s useful:

Laravel’s built-in unique rule doesn’t ignore soft-deleted rows by default.

This rule allows for cleaner validation logic in applications that use soft deletes.

Usage Example:

use App\Rules\UniqueWithTrashed;

$request->validate([
    'name' => ['required', new UniqueWithTrashed('zones', 'name')],
]);

This checks that the name column in the zones table is unique, excluding any soft-deleted rows.

@yaminlam yaminlam closed this Aug 20, 2025
@yaminlam yaminlam reopened this Aug 20, 2025
@taylorotwell
Copy link
Member

You can use Rule::unique for this.

@yaminlam
Copy link
Author

You can use Rule::unique for this.

The built-in unique rule does not automatically ignore soft-deleted records. This means if a record exists with a non-null deleted_at, the default unique validation would still fail.

My UniqueWithTrashed rule automatically ignores soft-deleted items, making it easier to enforce uniqueness only among active records without repeating custom query logic each time. It’s especially helpful in applications that heavily use soft deletes.

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.

2 participants