Laravel Query Builder其中为NULL无结果

时间:2017-12-12 02:00:42

标签: php laravel laravel-5 query-builder

我有以下代码来获取pdf_url不是NULL的货件数据;

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

这没有问题,我得到了我需要的数据,但是当我尝试使用相同的代码来获取pdf_url的数据为NULL时,它没有结果。

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

我错过了什么?我非常确定DB记录在那里。我也试过其他格式,但仍然没有结果;

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

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

编辑:我可以使用whereRaw,但我更愿意使用where。以下代码没有问题;

$shipment_data = DB::table('shipment')
 ->whereRaw('shipment_date = "2017-12-11" AND pdf_url is NULL')->get();

3 个答案:

答案 0 :(得分:3)

使用whereNull

$shipment_data = DB::table('shipment')
            ->whereNull('pdf_url')->get();

答案 1 :(得分:1)

试试这个:

 $records = DB::table('shipment')->where('shipment_date','2017-12-11)
              ->whereNull('pdf_url')->get();

答案 2 :(得分:0)

您可以使用whereNull

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

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