Laravel whereNotIn is not working when null values encountered

Viewed 18

I have a table (orders) where I have a 'status' column which allows null values. Here is an example of the table.

id status
1 'Draft'
2 'Draft'
3 null
4 'Draft'

when I write query like

 Order::whereIn('status',['Draft'])->count()

it gives me expected number which is 3.

But when I revert the query like.

Order::whereNotIn('status',['Draft'])->count()

It don't give me the expected answer which is 1. Rather it gives me always 0. Can you please help me out?

1 Answers

Give it a try:

DB::connection()->enableQueryLog();

Order::whereNotIn('status',['Draft'])->orWhereNull('status')->count();

DB::getQueryLog();
Related