沃梦达 / 编程问答 / php问题 / 正文

Laravel 在更新时更改 created_at

Laravel changes created_at on update(Laravel 在更新时更改 created_at)

本文介绍了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() 命令创建不可为空的时间戳字段.通过使用 $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->timestamps,而是使用 $table->timestamp('updated_at');$table->timestamp('created_at'); 你自己.确保您的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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Laravel 在更新时更改 created_at

基础教程推荐