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

cosminc's avatar

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

Hello,

I have the query that you can read bellow:

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

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.

0 likes
4 replies
RamjithAp's avatar

Add this to your query

whereNotNull("column_name")
bobbybouwmann's avatar
Level 88

That's because you either need to use orWhereRaw or pass in the 2 as the argument

$query->where('other_status', 0)
    ->orWhere([
        ['other_status', 2],
        [DB::raw('DATEDIFF(now(), other_status_updated_at)'), '<=', 2]
    ]);

// OR
$query->where('other_status', 0)
    ->orWhere('other_status', 2),
    ->orWhereRaw('DATEDIFF(now(), other_status_updated_at) <= 2');
2 likes
cosminc's avatar

Thank you @bobbybouwmann, using [DB::raw('DATEDIFF(now(), other_status_updated_at)'), '<=', 2] does the trick :)

Please or to participate in this conversation.