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
Laravel Query Builder - Set Column to NULL
提问by dcolumbus
I'm trying to update my column as NULL
but the value is passed as a string, and set the column to 0
instead 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);