【问题标题】:Sometimes give an error updating value on Laravel有时在 Laravel 上给出错误更新值
【发布时间】:2020-11-26 16:26:14
【问题描述】:

我执行了一个使用status = 1 更新整个数据库(有一些限制)的查询,并得到以下错误:

第一次执行:

Conversion failed when converting date and/or time from character string. (SQL: update [example] set [status] = 1, [updated_at] = 2020-11-26 14:13:38.1000 where [exampleId] = 1251)

第二次执行:

Conversion failed when converting date and/or time from character string. (SQL: update [example] set [status] = 1, [updated_at] = 2020-11-26 14:14:47.1000 where [exampleId] = 758) 

第三次执行:

No errors.

这似乎与updated_at 列有关,但我没有手动更改该值。


更新函数:

$example= $this->dbExample->where(...)->first();
$example->status = true;
$example->save();

模型类:

class Example extends Model
{
    use SoftDeletes;
    
    protected $table = 'example';
    protected $primaryKey = 'exampleId';
    protected $fillable = [...];
    protected $hidden = ['created_at', 'status', 'updated_at'];

}

我该如何解决这个问题?


编辑 1:

它总是在以.1000 结尾的时间戳上报错。

【问题讨论】:

  • 我们在这里遗漏了很多信息,也许你可以给我们你用来更新的代码?您要更新的表的模型?
  • 日期可能需要用引号引起来
  • @MarwaneEzzaze 添加。
  • @sta 对迟到的答案感到抱歉,它们都不起作用,但是您的评论让我想到了使用 protected $dateFormat = 'Y-m-d H:i:s.000'; 似乎有效的想法。谢谢!
  • 我会继续跟踪日志,如果它没有给出更多错误,我会的。

标签: php sql-server laravel


【解决方案1】:

尝试将#dates 数组添加到您的模型中:

class Example extends Model
{
    use SoftDeletes;
    
    protected $table = 'example';
    protected $primaryKey = 'exampleId';
    protected $fillable = [...];
    protected $hidden = ['created_at', 'status', 'updated_at'];
    protected $dates = [
        'updated_at',
        'created_at',
        'deleted_at',
    ];

}

【讨论】:

  • Conversion failed when converting date and/or time from character string. (SQL: update [example] set [status] = 1, [updated_at] = 2020-11-26 14:55:57.1000 where [exampleId] = 1293)
猜你喜欢
  • 2016-07-22
  • 1970-01-01
  • 1970-01-01
  • 2020-10-20
  • 2021-07-20
  • 1970-01-01
  • 2019-05-06
  • 1970-01-01
  • 2017-06-11
相关资源
最近更新 更多