Laravel - 一般错误:1366 不正确的整数值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40171980/
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 - General error: 1366 Incorrect integer value
提问by Marco
When I try to save or update a model, I get an error that is below, for the field spid_id. I am not sure what is wrong.
当我尝试保存或更新模型时,对于 spid_id 字段,出现以下错误。我不确定出了什么问题。
General error: 1366 Incorrect integer value: '' for column 'spid_id' at row 1 (SQL: update
magazines
setspid_id
= ,updated_at
= 2016-10-21 08:28:46,summary
= whereid
= 8)
一般错误:1366 不正确的整数值:第 1 行的列 'spid_id' 的 ''(SQL:更新
magazines
集spid_id
= ,updated_at
= 2016-10-21 08:28:46,summary
= whereid
= 8)
This is how my table looks:
这是我的桌子的样子:
Schema::create('magazines', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('visual_id')->nullable();
$table->integer('spid_id')->nullable();
$table->timestamps();
});
I tried to change my spid_id to not be nullable in the DB, by making a migration, because I thought that might be a reason:
我试图通过进行迁移将我的 spid_id 更改为在数据库中不可为空,因为我认为这可能是一个原因:
Schema::table('magazines', function (Blueprint $table) {
$table->integer('spid_id')->change();
});
But the field still remained nullable.
但是该字段仍然可以为空。
This is my store function for create form:
这是我创建表单的商店功能:
$magazine = Magazine::create([
'name' => $request->input('name'),
'visio_link_prefix' => $request->input('visio_link_prefix'),
'spid_id' => $request->input('spid_id'),
'summary' => $request->input('summary'),
]);
回答by aleksejjj
You need to check spid_id
exist in your request before you save it. For example in your case it will looks like:
spid_id
在保存之前,您需要检查您的请求中是否存在。例如,在您的情况下,它看起来像:
'spid_id' => $request->has('spid_id') ? $request->input('spid_id') : NULL,
回答by Bhavesh B
Another way to create a record is by using eloquent objects
另一种创建记录的方法是使用 eloquent 对象
$magazine = new Magazine
$magazine->name = $request->input('name');
$magazine->visio_link_prefix = $request->input('visio_link_prefix')
$magazine->spid_id = $request->input('spid_id', null);
$magazine->summary = $request->input('summary');
$magazine->save();
This will make sure it automatically assign a null or default value which you may have specified in your model.
这将确保它自动分配您可能在模型中指定的空值或默认值。