7

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,826
  • 1
  • 24
  • 32

3 Answers3

17

Use whereNull

$shipment_data = DB::table('shipment')
            ->whereNull('pdf_url')->get();
sumit
  • 15,003
  • 12
  • 69
  • 110
3

try this:

$records = DB::table('shipment')
  ->where('shipment_date','2017-12-11')
  ->whereNull('pdf_url')
  ->get();
Peter Krebs
  • 3,831
  • 2
  • 15
  • 29
Ajay
  • 848
  • 8
  • 17
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