Level 104
You are using an Eloquent Builder concept whereHas, but the exists Rule executes a Query Builder, so there is no concept of relationships. You would then want to use a JOIN instead, but this is not supported by the exists Rule, so a Closure might be the solution:
'email'=> [
'required',
function ($attribute, $value, $fail) {
$userExists = User::where('email', $value)
->where('status', 'active')
->whereHas('Roles', fn($query) => $query->where('title', 'supervisor' ))
->exists();
if (! $userExists) {
$fail("A supervisor with the given email could not be found");
}
}
]
1 like