Laravel 查询生成器 - 将列设置为 NULL

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

Laravel Query Builder - Set Column to NULL

phplaravellaravel-4query-builder

提问by dcolumbus

I'm trying to update my column as NULLbut the value is passed as a string, and set the column to 0instead of null.

我正在尝试更新我的列,NULL但该值作为字符串传递,并将该列设置为0而不是 null。

$update = DB::table('users')->where('id', $primary_key)->update(array($column_name => $new_value));

How can I ensue that the column is change to NULL?

我怎样才能确保该列更改为NULL

回答by Pathros

Try the following:

请尝试以下操作:

$update = DB::table('users')->where('id', $primary_key)->update(
  array
  (
     $column_name => Input::has('FormFieldName') ? Input::get('FormFieldName') : null,
  )
);

回答by passioncoder

Check your mysql table and ensure that the column is nullable and defaults to null.

检查您的 mysql 表并确保该列可为空且默认为空。

This can be done in your migration files:

这可以在您的迁移文件中完成

$table->string('awesome_column')->nullable()->default(null);