Laravel 5.4 将 NULL 保存到数据库不起作用

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

Laravel 5.4 save NULL to database not working

mysqldatabaselaravellaravel-5laravel-5.4

提问by Ilario Engler

The following code results in an empty value inside the database and when I get it out of the database its an empty String.

以下代码在数据库中产生一个空值,当我将它从数据库中取出时,它是一个空字符串。

$customer->update(['company' => NULL]);

回答by Marcin Nabia?ek

You should consider 3 things:

你应该考虑3件事:

1) Make sure companycolumn in nullablein your table. If it's not it won't be possible to put nullin there.

1)确保在您的表格中companynullable。如果不是,则无法放入null其中。

2) Make sure you have in $fillableproperty of Customermodel column company

2)确保您拥有模型列的$fillable属性Customercompany

3) Verify you don't have any mutator in your Customermodel - so verify you don't have setCompanyAttributemethod that might change value automatically to empty string if it's set to null

3) 验证您的Customer模型中没有任何修改器- 因此请验证您没有setCompanyAttribute可能将值设置为 null 时自动将其更改为空字符串的方法

回答by Ilario Engler

SOLUTION

解决方案

If you want a field inside the database to be null and you do not use a mutator everything is fine.

如果您希望数据库中的某个字段为空并且不使用 mutator,那么一切都很好。

Are you using a mutator, in my case the following snippet.

您是否在使用 mutator,在我的情况下是以下代码段。

public function setCompanyAttribute($value)
{
    $this->attributes['company'] = ucfirst($value);
}

Then you need to check if $value is null and then set it manually to null otherwise it will be an empty string.

然后您需要检查 $value 是否为空,然后手动将其设置为空,否则它将是一个空字符串。

To do that automatically on every model you can set the empty string back to null inside your EventServiceProvider.

要在每个模型上自动执行此操作,您可以在 EventServiceProvider 中将空字符串设置回 null。

Event::listen('eloquent.saving: *', function ($eventName, array $data) {
    $attributes = [];

    foreach ($data as $model) {
        foreach( $model->getAttributes() as $key => $value ) {
            $attributes[$key] = (is_string($value) && $value === '') ? null : $value;
       }

        $model->setRawAttributes($attributes);
    }

    return true;
});

It's important to first check all attributes from the model and set it to a temporary array. Now inside the setAttribute() method on the model instance the checks are done if a mutator exists. But thats exactly what you do not want, because it would set all fields with a definied mutator to an empty sting. Instead use setRawAttribute so the old attributes are overwritten with the ones from the temporary array.

首先检查模型中的所有属性并将其设置为临时数组非常重要。现在在模型实例的 setAttribute() 方法中,检查是否存在 mutator。但这正是您不想要的,因为它会将具有定义的 mutator 的所有字段设置为空字符串。而是使用 setRawAttribute 以便旧属性被临时数组中的属性覆盖。

This is safe to do because if the checked value isn't an empty string the already existing value is taken for that field. Otherwise if it's an empty string then null is set.

这是安全的,因为如果检查的值不是空字符串,则该字段将采用现有的值。否则,如果它是一个空字符串,则设置为 null。

Hope it helps!

希望能帮助到你!