6

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();
sulaiman sudirman
  • 1,746
  • 1
  • 21
  • 30

3 Answers3

14

Use whereNull

$shipment_data = DB::table('shipment')
            ->whereNull('pdf_url')->get();
sumit
  • 14,108
  • 10
  • 62
  • 106
3

try this:

$records = DB::table('shipment')
  ->where('shipment_date','2017-12-11')
  ->whereNull('pdf_url')
  ->get();
Peter Krebs
  • 3,083
  • 2
  • 13
  • 28
Ajay
  • 770
  • 7
  • 16
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