0
votes

What's wrong with my validation code ?

request()->validate([
        'name'=>['required', Rule::unique('users', 'name')->where(function($query) {
            $query->withTrashed();
        })],
        'email'=>['required', Rule::unique('users')->where(function($query) {
            $query->withTrashed();
        })],
        'password'=>'required|min:6|max:20|confirmed',
        'g-recaptcha-response' => 'required|captcha',
    ]);

In my User model I declared use Illuminate\Database\Eloquent\SoftDeletes; and use SoftDeletes trait in the class.

But when I want to validate the data I have this error :

Call to undefined method Illuminate\Database\Query\Builder::withTrashed()

Thank's for help.

1

1 Answers

4
votes

The unique validation rule does work on a table and not a model. The rule itself has no knowledge about the User model, it only knows about the users database table.

For this reason, the validation engine does use an Illuminate\Database\Query\Builder instance and not an Illuminate\Database\Eloquent\Builder instance. Unfortunately, the normal query builder cannot work with scopes which prevents you from using withTrashed().

This isn't really an issue though because the global soft deleteable filter isn't even applied when using the unique validation rule. In other words, you don't have to do anything - deleted elements will already be considered anyway.