雄辩的模型不更新时间戳 [英] Eloquent model not updating updated_at timestamp

查看:77
本文介绍了雄辩的模型不更新时间戳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using Laravel 5.1. To make it simple, I have the following code

迁移:

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

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

And this is the code somewhere else I'm using

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

现在,根据Laravel文档

Now, according to the Laravel documentation


再次,updated_at时间戳将自动更新,因此无需手动设置其值

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

updated_at值应在表中更新。但是,这不会发生。

The updated_at value should be updated in the table. However, this is not happening.

仅在第一次插入时设置,而不会在更新时设置。当我手动操作时,像这样

It get's only set on the first insert, but not on updating. When I do it manually, like this

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

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

it works. However, the docs say this should happend automatically, so what is wrong here?

我已经在stackoverflow上搜索了一些网站,但是我找到了Laravel 3或4.1、4.2等的网站。

I've searched some sites on stackoverflow for this question, but the ones I found where for Laravel 3 or 4.1, 4.2 etc.

我该如何正确执行?

推荐答案

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

As mentioned in comments, if the model did not change the timestamps wont be updated. However if you need to update them, or want to check if everything is working fine use $model->touch() - more here

这篇关于雄辩的模型不更新时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆