Laravel错误地将日期时间存储到jpb和队列上的数据库中

时间:2018-07-20 20:14:27

标签: php laravel laravel-5.6

在将数据创建到数据库后,我看到错误的数据时间为created_atupdated_atphpMyAdmin

我在laravel Asia/tehran文件中设置了config/app,并且我的系统日期时间正确并已更新,当我使用now()登录carbon时,得到以下输出:

Carbon @1532117471 {#1179 ▼
  date: 2018-07-21 00:41:11.142496 Asia/Tehran (+04:30)
}

这对我的系统日期时间是正确的,created_atupdated_at的数据库日期时间是:2018-07-20 19:58:26

当我使用Laravel作业队列时会发生此问题

此时的时钟是00:41:11而不是19:58:26,我该如何解决此问题并解决该问题?

1 个答案:

答案 0 :(得分:0)

您正在寻找laravel的created_at和Updated_at。它们被用作根据您的幼虫的 country spending_type total_spending <chr> <chr> <int> 1 uk economic 880 2 uk military 1900 3 usa economic 7080 4 usa military 13900 时区创建时间的时间戳。 您该如何更改?

选项1.转到config/app并将时区更改为所需的时区。

选项2。使用mutators管理在创建的模型。

  

https://laravel.com/docs/5.6/eloquent-mutators#defining-a-mutator

在口才模型上,您只需创建:

config/app.php

注意:要触发此功能,您需要使用Eloquent的Create,如下所示:

public function setCreatedAtAttribute($value) {
  /*
  $value is the value it is going to initially be stored
  This date format is required due to mySql's timestamp restriction.
  However, you can create a date with Carbon, and as long as you format into that 
  structure Y-d-m H:i:s it will do just fine
  */
  $this->attributes['created_at'] = Date("Y-m-d H:i:s);
}