Laravel 1048 存储数据时列不能为 NULL
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24391744/
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 1048 Column cannot be NULL on storing data
提问by user1876234
When I'm trying to save a new contact I'm getting 1048 Column 'username' cannot be NULL
error. It's pretty obvious that cause of this error is empty value of username
, however I'd like to make it work without setting a column to NULL or checking if username
POST data is empty and then setting it's value to ''
.
当我尝试保存新联系人时1048 Column 'username' cannot be NULL
出现错误。很明显,此错误的原因是 的空值username
,但是我想让它工作而不将列设置为 NULL 或检查username
POST 数据是否为空然后将其值设置为''
。
I've seen numerous of examples, where column is not set to NULL and/or data is not set to ''
before saving to database.
我见过许多示例,其中列未设置为 NULL 和/或数据''
在保存到数据库之前未设置为。
Or maybe I'm wrong and I missed something ?
或者也许我错了,我错过了什么?
I hope someone could comment on that..
我希望有人可以对此发表评论..
$contact = new Contact;
$contact->name = Input::get('name');
$contact->username = Input::get('username');
$nerd->save();
回答by Dave
Set default non-null values for the Input variable(s).
为输入变量设置默认的非空值。
$contact = new Contact;
$contact->name = Input::get('name');
$contact->username = Input::get('username', false);
$nerd->save();
回答by vikas
Storing value 0 to the username
将值 0 存储到用户名
$contact = new Contact;
$contact->name = Input::get('name');
$contact->username = Input::get('username', false);
$nerd->save();
回答by Ziaur Rahman
I have faced the same problem. The 'field name' in view page and the column name of database was indifferent. My problem was:
我遇到了同样的问题。视图页面中的“字段名称”与数据库的列名称无关。我的问题是:
"Integrity constraint violation: 1048 Column 'MoneyMethod' cannot be null".
My view page input tag:
我的视图页面输入标签:
{!! Form::select("MoneyMethod", $MoneymethodInfo, null,["class"=>"form-control MoneyMethod required","id"=>"MoneyMethod"]) !!},
notice the spelling of 'MoneyMethod' in my database table $table->string('MoneyMethod', 100);
the spelling of money method is same as view page but in my controller
注意我的数据库表中“MoneyMethod”$table->string('MoneyMethod', 100);
的拼写,money 方法的拼写与视图页面相同,但在我的控制器中
$riskfund->Moneymethod = Input::get('Moneymethod');
look carefully the spelling of 'Moneymethod' differs from view page and database tabel column. After correcting the spelling, it is working now.
仔细查看“Moneymethod”的拼写与查看页面和数据库表列不同。更正拼写后,它现在可以工作了。
So, please check the spelling of 'username' in form, controller, database table.
因此,请检查表单、控制器、数据库表中“用户名”的拼写。
回答by yogurt
You can use model events (http://laravel.com/docs/eloquent#model-events).
您可以使用模型事件 ( http://laravel.com/docs/eloquent#model-events)。
When a Contact is creating
or updating
, you can test if username is null, and change it to ''
before writing it to DB.
当 Contact 为creating
或 时updating
,可以测试 username 是否为空,并''
在写入数据库之前将其更改为 。