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

murilo's avatar
Level 10

Laravel Request with Rule::exists and whereHas

hello , I have a laravel Request for login . like this

this works - Get user , where status is = 'active'

 'email'=> ['required',  Rule::exists('user', 'email')->where(function ($query)  {
                $query->where('status', 'active');
            })],

does have how to do something like this ? Get user , where status is = 'active' AND Role = 'supervisor'

 'email'=> ['required',  Rule::exists('user', 'email')->where(function ($query)  {
                $query->where('status', 'active')->whereHas('Roles', function ($q) {
                    return $q->where('title', 'supervisor' );
                });
            })],
0 likes
1 reply
tykus's avatar
tykus
Best Answer
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

Please or to participate in this conversation.