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

User1980's avatar

Query Builder problem with 'where'

Hi,

I am having a problem. I have a very long query with multiple joins.

At the end of my query builder I have this

                ->whereIn('ad.ad_type', $adType)
                ->orWhereIn('ad.ad_one_id', $liveAdOne)
                ->orWhereIn('ad.ad_two_id', $liveAdTwo)
                ->orWhere('ad.status', '=', 'active')

I have 2 problems:

  1. The ->orWhereIn('ad.ad_two_id', $liveAdOne) might get from time to time and empty value(the query seems to fail on empty.

2)The last query has zero effect on the rest of the queries: ->orWhere('ad.status', '=', 'active')

I have 2 states. 'active', 'inactive', the where clause still pull both in the results.

I was going to use "when" as a subquery but I am unsure about something:

                ->whereIn('ad.ad_type', $adType)
                ->orWhereIn('ad.ad_one_id', $liveAdOne)
                ->when(!empty($liveAdTwo), function ($query) use ($liveAdTwo) {
                    ->orWhereIn('ad.ad_two_id', $liveAdTwo)
                })
                ->orWhere('ad.status', '=', 'active')   

The above query is still not fixing the ->orWhere('ad.status', '=', 'active') having no effect but at least is fixing the error of having $liveAdTwo as a possible empty value .

Any idea what is wrong please?

0 likes
7 replies
guybrush_threepwood's avatar

Hi @user1980

You can group your where conditions like this:

        $builder
            ->where(function($q) use ($adType, $liveAdOne, $liveAdTwo) {
                $q
                    ->whereIn('ad.ad_type', $adType)
                    ->orWhereIn('ad.ad_one_id', $liveAdOne)
                    ->when(!empty($liveAdTwo), function ($query) use ($liveAdTwo) {
                        return $query->orWhereIn('ad.ad_two_id', $liveAdTwo);
                    })
                ;
            })
            ->where('ad.status', '=', 'active')
User1980's avatar

I have noticed something..... If I change the order of the queries and put the most important at the top, in this case:

->where('ad.status', '=', 'active')
``

It seems to reclassify all the ads based on this query first.


Also based on your solution:

->where(function($q) use ($adType, $liveAdOne, $liveAdTwo) { })



Why grouping the queries would help? What would grouping them do actually please? What would become of the priority order for the group of query vs the last part of the query ->orWhere('ad.status', '=', 'active') ?

Thanks.
guybrush_threepwood's avatar

See this tutorial on combining AND/OR conditions in MySQL: https://www.techonthenet.com/mysql/and_or.php

Basically your code does this:

WHERE ad_type IN  (1, 2, 3)
OR ad_one_id IN (1, 2, 3)
OR ad_two_id IN (1, 2, 3)
AND status = 'active'

Which has a very different outcome when you group the conditions:

WHERE (
    ad_type IN  (1, 2, 3) OR
    ad_one_id IN (1, 2, 3) OR
    ad_two_id IN (1, 2, 3)
)
AND status = 'active'
User1980's avatar

Thanks for the reply. I think my issue is the confusion with 2 x where() and where followed by orWhere().

What would be the difference between this: ->where() ->where()

and ->where() ->orWhere()

is the orWhere interpreted as:

If the first "where" exists, query it, if not then query the other one below.

I am not sure how to interpret this.

Thank you.

CorvS's avatar
CorvS
Best Answer
Level 27

@user1980 The difference is that

->where()->where()

gets you all entries, that match both conditions

->where()->orWhere()

on the other hand gives you the entries, that match one of the conditions.

So your query from above

->whereIn('ad.ad_type', $adType)
->orWhereIn('ad.ad_one_id', $liveAdOne)
->orWhereIn('ad.ad_two_id', $liveAdTwo)
->orWhere('ad.status', '=', 'active')

gives you all ads, that match one of your conditions. That's why you get "inactive" ads too, because for them one of the other conditions matched. @guybrush_threepwood already explained how you can change the outcome of your query to satisfy your needs.

2 likes
User1980's avatar

Thank you so much and this is exactly my mistake.... I thought where and where with eloquent or the query builder was written as where orWhere(), I did find this weird to be honest, I decided to install telescope and look at all the queries and realised that where orWhere() is a "OR" and not "AND" as I wanted :-)

1 like

Please or to participate in this conversation.