gpt4 book ai didi

php - Eloquent 模型不更新 updated_at 时间戳

转载 作者:IT王子 更新时间:2023-10-29 00:00:54 26 4
gpt4 key购买 nike

我正在使用 Laravel 5.1。为了简单起见,我有以下代码

迁移:

Schema::create('sitemap_data', function (Blueprint $table) {
// Primary and foreign keys
$table->increments('id');
$table->string('postUrl');
// Database functions
$table->timestamps();
});

这是我在其他地方使用的代码

$sitemapData = SitemapData::firstOrNew([
'postUrl' => $post
]);
$sitemapData->save();

现在,根据 Laravel 文档

Again, the updated_at timestamp will automatically be updated, so there is no need to manually set its value

updated_at 值应该在表中更新。然而,这并没有发生。

它只在第一次插入时设置,而不是在更新时设置。当我手动做的时候,像这样

$sitemapData = SitemapData::firstOrNew([
'postUrl' => $post
]);
$sitemapData->updated_at = Carbon::now();
$sitemapData->save();

它有效。但是,文档说这应该是自动发生的,那么这里有什么问题呢?

我已经在 stackoverflow 上搜索了一些网站来解决这个问题,但我在 Laravel 3 或 4.1、4.2 等网站上找到了这些网站。

我该如何正确地做到这一点?

最佳答案

如评论中所述,如果模型未更改,则不会更新时间戳。但是,如果您需要更新它们,或者想检查是否一切正常,请使用 $model->touch() - 更多 here

关于php - Eloquent 模型不更新 updated_at 时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30844792/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com