Laravel 查询生成器,其中 NULL 没有结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47764275/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:05:23  来源:igfitidea点击:

Laravel Query Builder where is NULL no result

phplaravellaravel-5query-builder

提问by sulaiman sudirman

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

我的代码如下得到出货数据,其中pdf_urlNULL;

$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_urlis NULL, it has no result.

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

$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 whereinstead. Code below has no issue;

编辑:我可以使用whereRaw,但我更喜欢使用where。下面的代码没有问题;

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

回答by sumit

Use whereNull

使用 whereNull

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

回答by Ajay

try this:

尝试这个:

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

回答by Soji Carnegie Joseph

You can use whereNull

您可以使用 whereNull

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

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

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