php 在 Laravel 数据库查询中处理空值?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14982047/
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-08-25 08:18:41  来源:igfitidea点击:

Dealing with null values in Laravel database query?

phpmysqlnulllaravellaravel-4

提问by Hailwood

I am finding that I often need to select a field, based on a condition other than the id.

我发现我经常需要根据 id 以外的条件选择一个字段。

So, $user = User::where('last_login', $lastLogin)->where('last_warning', $lastWarning)->get();works perfectly.

所以,$user = User::where('last_login', $lastLogin)->where('last_warning', $lastWarning)->get();工作完美。

That is until you set one of the where's to allow nulls (let's do last_login).

直到您设置允许空值的位置之一(让我们做last_login)。

That is, it can either have a value or be null.

也就是说,它可以有一个值,也可以为空。

That means you need to use one of two function where()or whereNull()and to do that you need to break the chain, so it becomes

这意味着您需要使用两个功能之一,where()或者whereNull()要做到这一点,您需要打破链条,因此它变成

$user = User::where('last_warning', $lastWarning);

is_null($lastLogin) ? $user->whereNull('last_login') : $user->where('last_login', $lastLogin);

$user = $user->get();

I am wondering if wherehas a way to deal with this? as currently if you pass nullthrough to where you get where column = nullwhich doesn't work!

我想知道是否where有办法解决这个问题?就像目前一样,如果您null通过到您到达的地方where column = null,则不起作用!

回答by Laurence

Two options:

两种选择:

Option 1:

选项1:

if (is_null($lastLogin))
{
    $user = User::whereNull('last_login')->where('last_warning', $lastWarning)->get();
}
else
{
    $user = User::where('last_login', $lastLogin)->where('last_warning', $lastWarning)->get();
}

Option 2:

选项 2:

$user = User::where('last_login', (is_null($lastLogin) ? 'IS' : '=') ,$lastLogin)->where('last_warning', $lastWarning)->get();

Option two makes the query 'where last_login = x'or 'where last_login IS null'

选项二使查询 'where last_login = x'或 ' where last_login IS null'

回答by André Luiz Müller

You can try this:

你可以试试这个:

User::where(function($query) use ($lastlogin)
        {
            if(is_null($lastLogin))
            {
                $query->whereNull('last_login');
            }
            else
            {
                $query->where('last_login', '=', $lastLogin);
            }
        })->get();

It is a good solution when dealing with long queries with more than one parameter.

在处理具有多个参数的长查询时,这是一个很好的解决方案。

回答by Marco Aurélio Deleu

As of Laravel 5.3 you are now able to ->where('column', null)to automatically produce WHERE column IS NULL.

从 Laravel 5.3 开始,您现在可以->where('column', null)自动生成WHERE column IS NULL.

If using variable, make sure that they have PHP nullstrict value.

如果使用变量,请确保它们具有 PHPnull严格值。

回答by Gabriel Koerich

You can use DB::raw() as well:

您也可以使用 DB::raw() :

User::where('last_login', 'IS', DB::raw('null'))->where_last_warning($lastWarning)->get();