php SQLSTATE[22007]:无效的日期时间格式:1366 不正确的整数值:Laravel 中的“column_name”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40989702/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:17:57  来源:igfitidea点击:

SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: 'column_name' in Laravel

phpmysqllaravelpivot-tablelaravel-5.3

提问by Al-Amin

Larael Multiple Data Insert Error

Larael 多数据插入错误

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_idfield in nullable();Please someone help me Here column_name=unit_id

但我的unit_id领域nullable();请有人帮我在这里column_name=unit_id

回答by Jens

null is different than not existend. If you want to set null as a value you have to write it in your query:

null 与不存在不同。如果要将 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.

日期时间格式也是错误的。您必须将其作为字符串输入。

回答by Parithiban

You are using unit_id is it referenced with units(id) ?. You are inputing a empty value for a foregin key referenced column.use nulland not ''

您正在使用 unit_id 是否引用了 units( id) ?您正在为外键引用的列输入一个空值。使用 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);

回答by AddWeb Solution Pvt Ltd

Set 0 for unit_idbefore query, if it null/empty. See example:

unit_id如果查询之前为null/empty,则设置 0 。见示例:

if(!isset($unit_id) || empty($unit_id)) $unit_id = 0;
.
.
//insert query rest code

回答by Kaloyan Drenski

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();

  }

回答by jibril90

I was encountering the same error. update_at and created_at are automatically created by migration. 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.

我遇到了同样的错误。update_at 和 created_at 是由迁移自动创建的。备份您的模型并将其删除。然后使用 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

回答by saber tabatabaee yazdi

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);