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

标签 php laravel timestamp eloquent

我正在使用 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/

相关文章:

php - MySQL 查询 : generate all the people that are in the same classes at the same school as the current user

php - 在魔术引号字符串中回显类属性的语法

php - 有没有办法在 PHP 中追溯创建基于时间/节点的 UUID?

php - 按时间戳对 MySQL 表进行排序

Laravel 在 Eloquent 中使用 with() 方法时出错

c# - System.Exception 是否有内置的抛出时间戳?

php - 使用 Laravel 的 session 数据库

PHP:如何重命名使用 Zend_Form_Element_File 上传的文件?

php - laravel 中的数据库逻辑在哪里?

mysql - 使用嵌套子句对查询结果进行排序