1366不正确的整数值:Laravel 4 [英] 1366 Incorrect integer value: Laravel 4
问题描述
我不断得到Incorrect integer value: '' for column 'stock'
这是我迁移stock
$table->integer('stock')->nullable()->default(0)->unsigned();
这是我要如何将其存储到库存中
Here is how I grab it to store into stock
$inventory->stock = $value[2];
//一些数据是''=空字符串
$inventory->stock = $value[2];
// some data are ''= empty string
是否可以为其设置默认值? 如何停止此错误消息?
Is there a way to set a default value for it ? How do I stop this error message ?
推荐答案
在尝试保存数据之前检查数据.从您的示例来看,看起来您在字符串中将有一个整数值,因此您可以执行例如这个:
Check the data before trying to save it. From your example, it looks like you'll have an integer value within a string, so you could do e.g. this:
$inventory->stock = ctype_digit($value[2]) ? $value[2] : 0;
如果不为0,则为null,或其他一些默认值.
If not 0, then null, or some other default value.
或者,即使您确定这些值是空字符串还是整数,也可以这样做:
Or even this if you're sure about the values either being empty string or integers:
$inventory->stock = $value[2] ?: 0;
如果您没有库存,也可能不添加库存,因为无论如何您都有该列的默认值.
You could also probably just not add the stock if you don't have any, since you have a default for the column anyway.
这篇关于1366不正确的整数值:Laravel 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!