Framework: orWhereNull inside whereHas disables the whereHas

Created on 19 Jan 2017  路  6Comments  路  Source: laravel/framework

  • Laravel Version: 5.1.45
  • PHP Version: 7.0
  • Database Driver & Version: MySQL, MariaDB Server 10.1.18

Description:

It seems to me that orWhereNull works incorrectly inside a whereHas. It seems to cancel all of whereHas effect.

Steps To Reproduce:

This was my code:

$items = Item::whereHas('owner', function($owner) {
    $owner->where('status', '=', 5)->orWhereNull('status');
})->get();

I got $items that contained all of the items regardless of owner's status.

I got the desired result by wrapping the conditions inside another closure like this:

$items = Item::whereHas('owner', function($owner) {
    $owner->where(function($o) {
        $o->where('status', '=', 5)->orWhereNull('status');
    });
})->get();

I am not sure if this is a bug or the intended behaviour, I just wanted to let you know in case this is an unknown error.

Most helpful comment

@tontonsb 5.1 I understand your frustration. In my opinion i think it should be addressed. But i also noticed in that pull request It was asked it it could be merged into 5.1. Graham said that it wouldn't be merged into 5.1 as it only receives "big fixes". Given the fact that in order to write the above query you need to nest it. I think thats a big enough deal to have it resolved or at least documented that you have to do that.. which i think the latter is kind of sucky.

All 6 comments

Change log shows that this pull request #13794 for v5.2.36 resolved this by automatically wrapping the clauses.

Your second example is perfectly acceptable for 5.1

@arjasco If this was resolved for 5.2, shouldn't it also be resolved for 5.1 as it is still supported?

Of course, acceptable workarounds exist, it just seems natural to expect the first example to work.

@tontonsb 5.1 I understand your frustration. In my opinion i think it should be addressed. But i also noticed in that pull request It was asked it it could be merged into 5.1. Graham said that it wouldn't be merged into 5.1 as it only receives "big fixes". Given the fact that in order to write the above query you need to nest it. I think thats a big enough deal to have it resolved or at least documented that you have to do that.. which i think the latter is kind of sucky.

You can probably get changes into 5.1 so long as they're not controversial. We don't want to break behaviour that people were relying on, even if the new behaviour is better.

I'd recommend upgrading to Laravel 5.4. The LTS release is a good stepping stone for getting off Laravel 4, but I'd recommend tracking the latest stable.

We have this problem in Laravel 5.5 but with WhereIn.

When using orWhereNull the query ignores WhereIn clause because Eloquent does not place the OR properly.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

YannPl picture YannPl  路  3Comments

progmars picture progmars  路  3Comments

ghost picture ghost  路  3Comments

JamborJan picture JamborJan  路  3Comments

kerbylav picture kerbylav  路  3Comments