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

noblemfd's avatar

Laravel Query with multiple where clause

In my Laravel-5.8, I have this query:

$companies = $this->getCompanies();
$company_credentials = OrgSocialiteSetting::where('id', $companies->id)->first();

$azureUser = Socialite::with('azure')->user();

$user = User::where('company_id', $company_credentials->company_id)->where('email', $azureUser->email)->orWhere('username', $azureUser->user['mailNickname'])->first();

I have two where and one orWhere

I want to tie where('email', $azureUser->email)->orWhere('username', $azureUser->user['mailNickname']) together.

How do I achieve that?

Thanks

0 likes
4 replies
MichalOravec's avatar
Level 75

Have you ever read a documentation?

$user = User::where('company_id', $company_credentials->company_id)->where(function ($query) use ($azureUser) {
    $query->where('email', $azureUser->email)
            ->orWhere('username', $azureUser->user['mailNickname']);
})->first();

Docs: https://laravel.com/docs/5.8/queries#parameter-grouping

MichalOravec's avatar

I fixed it.

Do you have still a problem to find what the error is?

After a few months I don't see any progress with your programing skills...

Please or to participate in this conversation.