問題描述
我在這個主題上找到了這個答案,但它沒有不適合我.
I found this answer on the subject, but it doesn't work for me.
所以,我在數(shù)據(jù)庫中創(chuàng)建一個條目:
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 |
然后我向外部服務器發(fā)出請求,我需要更新該行:
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?
編輯
我需要一個解決方案,它只修改行為而不刪除列或重置遷移.必須在不接觸數(shù)據(jù)的情況下在實時數(shù)據(jù)庫上執(zhí)行修復.如下所示,我嘗試了以下遷移:
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.
- 更新 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.
- 使用可為空的時間戳:
將 $table->timestamps()
更改為 $table->nullableTimestamps()
.默認情況下,$table->timestamps()
命令創(chuàng)建不可為空的時間戳字段.通過使用 $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.
- 自己定義時間戳:
不要使用 $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的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!