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

FounderStartup's avatar

Is it the correct way to use orWhere ?

$totaldealsinprogress = Listings::where('dealrealtor_id',$user->id)->orWhere('user_id', $user->id)->where('status',1)->where('dealstatus',2)->count();

I need records which has ( ( ('dealrealtor_id',$user->id) OR ('user_id', $user->id)) AND ('status',1) AND ('dealstatus',2) )

Am I using the orWhere correctly ? I am not getting the desired results with this query.

0 likes
19 replies
Tray2's avatar

When mixing AND and OR you might need to group them together to get the desired result.

See this example from the docs.

$users = DB::table('users')
            ->where('votes', '>', 100)
            ->orWhere(function($query) {
                $query->where('name', 'Abigail')
                      ->where('votes', '>', 50);
            })
            ->get();

https://laravel.com/docs/9.x/queries#or-where-clauses

1 like
Tray2's avatar

@FounderStartup These two needs to be grouped

where('dealrealtor_id',$user->id)->orWhere('user_id', $user->id)
1 like
FounderStartup's avatar

@Tray2 Thanks again. But how ? How to group with OR ?

This is my code :

$totaldealsinprogress = Listings::where('dealrealtor_id',$user->id)->orWhere('user_id', $user->id)->where('status',1)->where('dealstatus',2)->count();
frankielee's avatar
Level 29

@FounderStartup

Have you tried to group your code like the following?


 Listings::where(function ($query)  use ($user){
	$query->where('dealrealtor_id',$user->id)->orWhere('user_id', $user->id);
})->where('status',1)->where('dealstatus',2)->count();
1 like
MichalOravec's avatar

@FounderStartup

Yes but my code is not working. The example is different than what I need :) Can anybody help ?

Stop asking for a code! The example in the documentation is pretty clear.

People please stop doing things instead of OP!

FounderStartup's avatar

@MichalOravec Here you go again . A negative person can never stop doing negative comments. If everything is already there in the documentation then why we need this platform ?

Kindly stay away as I advised and refrain from posting your negative comments. Don't spoil this excellent platform.

MichalOravec's avatar

@FounderStartup What is the difference between

$users = DB::table('users')
           ->where('name', '=', 'John')
           ->where(function ($query) {
               $query->where('votes', '>', 100)
                     ->orWhere('title', '=', 'Admin');
           })
           ->get();

and

 Listings::where(function ($query)  use ($user){
	$query->where('dealrealtor_id',$user->id)->orWhere('user_id', $user->id);
})->where('status',1)->where('dealstatus',2)->count();

in logical grouping? Nothing, it's the same.

It was really difficult? Unbelievable... Leave this forum if you have no effort to do something yourself.

1 like
FounderStartup's avatar

@MichalOravec I am already doing. But negative people like you should not be allowed on this platform. Stop ridiculing people ( I have seen many of your messages on this platform where you have done this again and again with many developers ). Don't lose your respect like this.

Simply ignore the post if you have these sudden rush of negativity. Keep your negativity to yourself . It will be better for you. A matured software engineer will never comment the way you are doing. Grow up now.

FounderStartup's avatar

@MichalOravec I repeat again. Why we need a platform like this when everything is there in the documentation ??? People like you will never understand.

FounderStartup's avatar

@MichalOravec There are so many post. You even asked somebody to stop coding :) Anyway ! It's your life . But I again suggest that stop ridiculing people. You will get respect only when you respect people. Simply ignore the post which you don't like. Thanks.

FounderStartup's avatar

@MichalOravec I am asking for help because I missed something. Do you think anybody can develop a site by keep asking for code on laracast ? People ask for help when they are unable to resolve a bug. And that's the whole purpose of laracst and other similar sites. If you simply say that everything is in the documentation then such platforms carry no value , but in real life its not like that. Reading documentation and developing a real life site are totally two different things. If people don't like to share or help then such post will never get any replies. If asking for help is not in the terms of the site then kindly share it and will leave immediately , but then 99% people will also leave :).

Please or to participate in this conversation.