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

ftf50wuq  于 2023-01-21  发布在  其他
关注(0)|答案(8)|浏览(87)

Laravel多数据插入错误
SQLSTATE [22007]:无效的日期时间格式:1366不正确的整数值:第2行的列"unit_id"的""(SQL:插入product_pricescreated_atproduct_idunit_idupdated_at)值(2016年12月6日06:56:01,27,1,2016年12月6日06:56:01),(2016年12月6日06:56:01,27,,2016年12月6日06:56:01)
但是我的nullable();中的unit_id字段请来人帮帮我这里column_name=unit_id

ffx8fchx

ffx8fchx1#

null不同于not existend。如果要将null设置为值,则必须将其写入查询中:

... ('2016-12-06 06:56:01',27, null, '2016-12-06 06:56:01'))

日期时间的格式也不对。你必须以字符串的形式输入。

33qvvth1

33qvvth12#

使用intval()函数可以很好地解决这个问题。

intval($unit_id);

在以下情况下,此代码将返回0

  • 未设置$unit_id
  • $unit_id是字符串,而不是字母数字(例如'abc')。
  • $unit_idNULL

如果$unit_id是字母数字字符串(例如'10'),它将返回数字。

yyyllmsg

yyyllmsg3#

只是有同样的问题,在我的情况下,这是一个愚蠢的错误,在我的控制器。
我所做的是返回整个对象而不是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();

  }
xwbd5t1u

xwbd5t1u4#

您正在使用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);
z3yyvxxp

z3yyvxxp5#

unit_id如果null/empty,查询前置0,示例:

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

ds97pgxw6#

我也遇到了同样的错误。update_at和created_at是迁移自动创建的。备份您的模型并删除它。然后使用php artisan创建一个新的模型使用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();
    });
}

然后做迁移php工匠迁移

soat7uwm

soat7uwm7#

我可以解决它,检查你的有效载荷时,发送请求把或张贴到你的API
我应该发送这个json:

{
...,
"item":1
}

但是我发了这个这是我的错

{
...,
"item":[{"id"=1}]
}

或{...,“项目”:[1] }
我在laravel的代码是:

$item= Item::find($request->input('item'));
   $client->item()->associate($item);
jmp7cifd

jmp7cifd8#

我通过使用验证器函数解决了这个问题,如下所示:

function if_set($v)
    if (isset($_POST[$v]) and $_POST[$v] != '')
        $data_to_db[$v] = $_POST[$v];

相关问题