Laravel Query Builder where is NULL no result [duplicate]
Asked Answered
T

3

8

I have code as below to get shipment data where pdf_url is not NULL;

$shipment_data = DB::table('shipment')->where([
 'shipment_date' => '2017-12-11', ['pdf_url', '<>', 'NULL']])->get();

This has no problem, I get the data I need, but when I'm trying to use the same code to get the data with pdf_url is NULL, it has no result.

$shipment_data = DB::table('shipment')->where([
 'shipment_date' => '2017-12-11', ['pdf_url', '=', 'NULL']])->get();

What do I missing? I am very sure the DB record is there. I also tried other formats but still no result;

$shipment_data = DB::table('shipment')->where([
 'shipment_date' => '2017-12-11', ['pdf_url', 'NULL']])->get();

And

$shipment_data = DB::table('shipment')->where([
 'shipment_date' => '2017-12-11', 'pdf_url' => 'NULL'])->get();

EDIT: I can use whereRaw, but I'll prefer to use where instead. Code below has no issue;

$shipment_data = DB::table('shipment')
 ->whereRaw('shipment_date = "2017-12-11" AND pdf_url is NULL')->get();
Tribadism answered 12/12, 2017 at 2:0 Comment(1)
Thanks @Ben thats probably the reason. I guess I have no choice to use whereNull or whereRaw insteadTribadism
A
18

Use whereNull

$shipment_data = DB::table('shipment')
            ->whereNull('pdf_url')->get();
Alit answered 12/12, 2017 at 2:5 Comment(3)
I'm aware of whereNull as well as whereRaw, I'm just wondering why it doesnt work when I'm using where with not NULL is okayTribadism
You can't compare NULL using =, <>, etc. but rather you must use IS NULL and IS NOT NULL.Quindecennial
@TimBiegeleisen pointed exactly the same thingAlit
I
4

try this:

$records = DB::table('shipment')
  ->where('shipment_date','2017-12-11')
  ->whereNull('pdf_url')
  ->get();
Imhoff answered 12/12, 2017 at 7:32 Comment(0)
E
1

You can use whereNull

The whereNull method verifies that the value of the given column is NULL.

https://laravel.com/docs/5.5/queries

Espinal answered 12/12, 2017 at 2:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.