5 months ago

Eloquent adds "is null" condition to a query and messes the results

Posted 5 months ago by cosminc


I have the query that you can read bellow:

Foo::where('some_status', 1)
            ->where(function ($query) {
                $query->where('other_status', 0)
                        ['other_status', 2],
                        [DB::raw('DATEDIFF(now(), other_status_updated_at) <= 2')]

This gets translated to:

select * from `foo` where `some_status` = ? and (`other_status` = ? or (`other_status` = ? and DATEDIFF(now(), other_status_updated_at) <= 2 is null))

I don't understand where that "is null" at the end is coming from and unfortunately it messes the results.

How can I change the query so I won't have the "is null" added?

Thank you.

Please sign in or create an account to participate in this conversation.