Laravel Eloquent created_at 正在更新
Laravel Eloquent created_at is getting updated
我刚遇到 Eloquent (Laravel 5.2.10) 默认时间戳的问题。默认情况下,$table->timestamps()
方法会在 table 中为您提供一个 created_at
和一个 updated_at
列,它们会相应更新。
但是,当我尝试一些测试条目时,我注意到当您更新记录时,updated_at
字段会更新,这是应该的,但是 created_at
字段 也 得到更新:它的小时保持在创建的时间,但分钟和秒得到更新以匹配 updated_at
字段。
如果这太罗嗦了,这里有一个例子:
+---------------------+---------------------+
| created_at | updated_at |
+---------------------+---------------------+
| 2016-01-13 17:13:27 | 2016-01-13 22:13:27 |
| 2016-01-13 16:14:41 | 2016-01-13 21:14:41 |
+---------------------+---------------------+
请注意 created_at
和 updated_at
字段的分钟和秒完全相同。
我试图查看设置时间戳的Model
class,但事实证明 setCreatedAt()
方法确实没有在仅更新记录时被调用。
为什么会发生这种情况?
如何 防止 created_at
字段发生变化? (更重要)
您的问题可能与这里的问题有关:https://github.com/laravel/framework/issues/11518
长话短说,很多人的 created_at
栏都有 ON UPDATE CURRENT_TIMESTAMP
属性。
如 Github 页面所述:
MySQL 5.7 no longer allows 0000-00-00 as a valid timestamp with
strict mode turned on (which it is by default). So either use
->nullableTimestamps()
or ->timestamp()->useCurrent()
.
因此,您可以通过更改以下内容来解决此问题:
$table->timestamps();
选择以下任一选项:
// Option 1:
$table->nullableTimestamps();
// Option 2:
$table->timestamp('updated_at')->useCurrent();
$table->timestamp('created_at')->useCurrent();
此外,在此 MySQL 页面上:https://dev.mysql.com/doc/refman/5.6/en/timestamp-initialization.html
Alternatively, if explicit_defaults_for_timestamp
is disabled (the default), do either of the following:
Define the column with a DEFAULT clause that specifies a constant default value.
Specify the NULL attribute. This also causes the column to permit NULL values, which means that you cannot assign the current timestamp by setting the column to NULL. Assigning NULL sets the column to NULL.
我刚遇到 Eloquent (Laravel 5.2.10) 默认时间戳的问题。默认情况下,$table->timestamps()
方法会在 table 中为您提供一个 created_at
和一个 updated_at
列,它们会相应更新。
但是,当我尝试一些测试条目时,我注意到当您更新记录时,updated_at
字段会更新,这是应该的,但是 created_at
字段 也 得到更新:它的小时保持在创建的时间,但分钟和秒得到更新以匹配 updated_at
字段。
如果这太罗嗦了,这里有一个例子:
+---------------------+---------------------+
| created_at | updated_at |
+---------------------+---------------------+
| 2016-01-13 17:13:27 | 2016-01-13 22:13:27 |
| 2016-01-13 16:14:41 | 2016-01-13 21:14:41 |
+---------------------+---------------------+
请注意 created_at
和 updated_at
字段的分钟和秒完全相同。
我试图查看设置时间戳的Model
class,但事实证明 setCreatedAt()
方法确实没有在仅更新记录时被调用。
为什么会发生这种情况?
如何 防止 created_at
字段发生变化? (更重要)
您的问题可能与这里的问题有关:https://github.com/laravel/framework/issues/11518
长话短说,很多人的 created_at
栏都有 ON UPDATE CURRENT_TIMESTAMP
属性。
如 Github 页面所述:
MySQL 5.7 no longer allows 0000-00-00 as a valid timestamp with strict mode turned on (which it is by default). So either use
->nullableTimestamps()
or->timestamp()->useCurrent()
.
因此,您可以通过更改以下内容来解决此问题:
$table->timestamps();
选择以下任一选项:
// Option 1:
$table->nullableTimestamps();
// Option 2:
$table->timestamp('updated_at')->useCurrent();
$table->timestamp('created_at')->useCurrent();
此外,在此 MySQL 页面上:https://dev.mysql.com/doc/refman/5.6/en/timestamp-initialization.html
Alternatively, if
explicit_defaults_for_timestamp
is disabled (the default), do either of the following:Define the column with a DEFAULT clause that specifies a constant default value.
Specify the NULL attribute. This also causes the column to permit NULL values, which means that you cannot assign the current timestamp by setting the column to NULL. Assigning NULL sets the column to NULL.