Laravel在更新时更改了created_at [英] Laravel changes created_at on update

查看:987
本文介绍了Laravel在更新时更改了created_at的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这个问题上找到了这个答案,但它没有不能为我工作.

I found this answer on the subject, but it doesn't work for me.

因此,我在数据库中输入了一个条目:

So, I make an entry in the database:

// Write lead to database
$lead = Lead::create($lead_data);

时间戳看起来像这样,很好:

And the timestamps look like this, which is good:

| 2016-01-08 10:34:15 | 2016-01-08 10:34:15 |

但是随后我向外部服务器发出了一个请求,我需要更新该行:

But then I make a request to an external server, and I need to update the row:

$lead->user_id = $response['user_id'];
$lead->broker_id = $response['broker_id'];
$lead->save();

和created_at字段被更改:

and the created_at field gets changed:

| 2016-01-08 04:34:17 | 2016-01-08 10:34:17 |

我该如何解决这个问题?

How do I solve this problem?

编辑

我需要一个解决方案,该解决方案只需修改行为,而无需删除列或重置迁移.该修复程序必须在实时数据库上执行,而无需接触数据.如下面的建议,我尝试了以下迁移:

I need a solution that would just modify the behavior without dropping columns or resetting migrations. The fix has to be performed on a live database without touching the data. As suggested below, I tried the following migration:

$table->datetime('created_at')->default(DB::raw('CURRENT_TIMESTAMP'))->change();

但是什么也没发生. created_at字段仍会在更新时被修改.

but nothing happens. The created_at field still gets modified on update.

推荐答案

如果您使用Laravel 5.2并使用MySQL,则时间戳中会引入一些错误".您可以在github 此处上阅读有关该问题的所有信息.它与时间戳的默认值有关,MySQL在特定条件下会自动分配DEFAULT CURRENT_TIMESTAMP或ON UPDATE CURRENT_TIMESTAMP属性.

If you're on Laravel 5.2 and using MySQL, there was a bit of a "bug" introduced with the timestamps. You can read all about the issue on github here. It has to do with the timestamp defaults, and MySQL automatically assigning DEFAULT CURRENT_TIMESTAMP or ON UPDATE CURRENT_TIMESTAMP attributes under certain conditions.

基本上,您有三个选择.

Basically, you have three options.

  1. 更新MySQL变量:

如果将explicit_defaults_for_timestamp变量设置为TRUE,则不会为时间戳列自动分配DEFAULT CURRENT_TIMESTAMP或ON UPDATE CURRENT_TIMESTAMP属性.您可以在此处中了解更多信息. >.

If you set the explicit_defaults_for_timestamp variable to TRUE, no timestamp column will be assigned the DEFAULT CURRENT_TIMESTAMP or ON UPDATE CURRENT_TIMESTAMP attributes automatically. You can read more about the variable here.

  1. 使用可为空的时间戳记:

$table->timestamps()更改为$table->nullableTimestamps().默认情况下,$table->timestamps()命令创建不可为null的时间戳字段.通过使用$table->nullableTimestamps(),您的时间戳字段将为空,并且MySQL不会自动为第一个字段分配DEFAULT CURRENT_TIMESTAMP或ON UPDATE CURRENT_TIMESTAMP属性.

Change $table->timestamps() to $table->nullableTimestamps(). By default, the $table->timestamps() command creates timestamp fields that are not nullable. By using $table->nullableTimestamps(), your timestamp fields will be nullable, and MySQL will not automatically assign the first one the DEFAULT CURRENT_TIMESTAMP or ON UPDATE CURRENT_TIMESTAMP attributes.

  1. 自己定义时间戳:

您可以自己使用$table->timestamp('updated_at'); $table->timestamp('created_at');,而不是使用$table->timestamps.确保您的"updated_at"字段是表中的第一个时间戳,以便将其自动分配为DEFAULT CURRENT_TIMESTAMP或ON UPDATE CURRENT_TIMESTAMP属性.

Instead of using $table->timestamps, use $table->timestamp('updated_at'); $table->timestamp('created_at'); yourself. Make sure your 'updated_at' field is the first timestamp in the table, so that it will be the one that is automatically assign the DEFAULT CURRENT_TIMESTAMP or ON UPDATE CURRENT_TIMESTAMP attributes.

这篇关于Laravel在更新时更改了created_at的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆