Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ynoth25's avatar

Laravel unique validation

Greetings!

I have this Request file that contains unique validation if email exists in table. I'm confused because even if the record is soft deleted validation does not ignore the row. Is it a correct behavior? What is best practice to validate and ignore soft deleted records

0 likes
6 replies
LaryAI's avatar
Level 58

To ignore soft deleted records during unique validation in Laravel, you can use the ignore validation rule. Here's an example of how you can modify your existing validation rule:

use Illuminate\Validation\Rule;

public function rules()
{
    $id = $this->route('id'); // assuming you're using route model binding

    return [
        'email' => [
            'required',
            'email',
            Rule::unique('users')->ignore($id)->whereNull('deleted_at')
        ]
    ];
}

In this example, we're using the ignore rule to exclude the record with the given $id from the unique check. Additionally, we're using the whereNull method to only consider records where the deleted_at column is null (i.e. not soft deleted).

Note that you'll need to adjust the table name and column name in the unique rule to match your specific use case.

ynoth25's avatar

@Muetze

is whereNull clause not enough? what are those ignoring id for?

Snapey's avatar

if you ignore soft deleted models (which is perfectly easy to do) what happens if that soft deleted model is restored? now you have duplicates.

Perhaps the user trying to use an existing email is actually trying to reinstate their account?

Perhaps you should obfuscate the original email address rather than soft delete it?

Please or to participate in this conversation.