[英]SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: 'column_name' in Laravel
Laravel Multiple Data Insert Error Laravel 多数据插入错误
SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: '' for column 'unit_id' at row 2 (SQL: insert into
product_prices
(created_at
,product_id
,unit_id
,updated_at
) values (2016-12-06 06:56:01, 27, 1,2016-12-06 06:56:01), (2016-12-06 06:56:01,27, , 2016-12-06 06:56:01))SQLSTATE[22007]:无效的日期时间格式:1366 不正确的整数值:第 2 行的列“unit_id”的“”(SQL:插入
product_prices
(created_at
、product_id
、unit_id
、updated_at
)值(2016-12-06 06:56: 01, 27, 1,2016-12-06 06:56:01), (2016-12-06 06:56:01,27, , 2016-12-06 06:56:01))
But my unit_id
field in nullable();
但是我在
nullable();
unit_id
字段nullable();
Please someone help me Here column_name=unit_id
请有人帮我这里
column_name=unit_id
null is different than not existend. null 与不存在不同。 If you want to set null as a value you have to write it in your query:
如果要将 null 设置为值,则必须在查询中写入它:
... ('2016-12-06 06:56:01',27, null, '2016-12-06 06:56:01'))
Also the datetime format is wrong.日期时间格式也是错误的。 You have to enter it as a string.
您必须将其作为字符串输入。
Set 0 for unit_id
before query, if it null/empty .在查询之前为
unit_id
设置 0,如果它为null/empty 。 See example:见示例:
if(!isset($unit_id) || empty($unit_id)) $unit_id = 0;
.
.
//insert query rest code
Just had the same issue and in my case it was a silly mistake in my controller.刚刚遇到了同样的问题,就我而言,这是我的控制器中的一个愚蠢的错误。
What I did was I returned the whole object instead of just the id, like so:我所做的是我返回了整个对象,而不仅仅是 id,如下所示:
public function store($id, Request $request) {
$post = Post::find($id);
$comment = new Comment;
$comment->text = $request->comment;
$comment->post_id = $post; <--- HERE IS THE MISTAKE
$comment->post_id = $post->id; <--- HERE IS THE FIX
$comment->user_id = Auth::id();
$comment->save();
return back();
}
You are using unit_id is it referenced with units( id
) ?.您正在使用 unit_id 是否引用了单位(
id
)?。 You are inputing a empty value for a foregin key referenced column.use null and not ''您正在为外键引用的列输入一个空值。使用null而不是''
insert into product_prices (created_at, product_id, unit_id, updated_at)
values (2016-12-06 06:56:01, 27, 1,2016-12-06 06:56:01),
(2016-12-06 06:56:01,27,null, 2016-12-06 06:56:01);
I was encountering the same error.我遇到了同样的错误。 update_at and created_at are automatically created by migration.
update_at 和 created_at 是由迁移自动创建的。 Backup your model and delete it.
备份您的模型并将其删除。 Then create a new Model using php artisan use php artisan make:model Model Name -m then in your migration table add the fields you need eg.
然后使用 php artisan use php artisan make:model Model Name -m 创建一个新模型,然后在您的迁移表中添加您需要的字段,例如。
public function up()
{
Schema::create('flights', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->string('airline');
$table->timestamps();
});
}
then do the migration php artisan migrate然后进行迁移 php artisan migrate
i can solve it, check your payload when sent request put or post to your api我可以解决它,在发送请求时检查您的有效负载放置或发布到您的 api
i should send this json:我应该发送这个json:
{
...,
"item":1
}
but i sent this.但我发送了这个。 this is my mistake
这是我的错误
{
...,
"item":[{"id"=1}]
}
or { ..., "item":[1] }或 { ..., "item":[1] }
my code in laravel is this:我在 Laravel 中的代码是这样的:
$item= Item::find($request->input('item'));
$client->item()->associate($item);
Using the intval()
function can solve the problem quite well.使用
intval()
函数可以很好地解决问题。
intval($unit_id);
This code will return 0
if:如果出现以下情况,此代码将返回
0
:
$unit_id
is not set. $unit_id
未设置。$unit_id
is string and not alphanumeric (eg 'abc'
). $unit_id
是字符串而不是字母数字(例如'abc'
)。$unit_id
is NULL
$unit_id
为NULL
If $unit_id
is alphanumeric string (eg '10'
), it'll return the number.如果
$unit_id
是字母数字字符串(例如'10'
),它将返回数字。
in my case it solved by using validator in insert() function pass your post name to this在我的情况下,它通过在 insert() 函数中使用验证器来解决,将您的帖子名称传递给这个
function if_set($v) if (isset($_POST[$v]) and $_POST[$v] != '') $data_to_db[$v] = $_POST[$v];函数 if_set($v) if (isset($_POST[$v]) 和 $_POST[$v] != '') $data_to_db[$v] = $_POST[$v];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.