php Eloquent 模型不更新 updated_at 时间戳

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30844792/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:06:22  来源:igfitidea点击:

Eloquent model not updating updated_at timestamp

phplaraveltimestampeloquent

提问by Musterknabe

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

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

Migration:

移民:

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();

Now, according to the Laravel documentation

现在,根据 Laravel 文档

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.

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

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?

有用。但是,文档说这应该自动发生,所以这里有什么问题?

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.

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

How would I do that correctly?

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

回答by Pawel Bieszczad

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

正如评论中提到的,如果模型没有改变时间戳不会更新。但是,如果您需要更新它们,或者想检查是否一切正常,请使用$model->touch()- 更多在这里

回答by Saeed

Yeah what Musterknabehas done is correct but he should also check his model SitemapData.phpit should have to have set $timestamps = true;

是的,Musterknabe所做的是正确的,但他还应该检查他的模型SitemapData.php,它应该设置$timestamps = true;

<?php 

 class SitemapData extends Eloquent {

        public $timestamps = true;
}

回答by Stan Smulders

It is possible actually:

实际上是可能的:

$model->updated_at = Carbon::now();
$model->save(['timestamps' => FALSE]);

That will properly save the updated_atto now. If you're not sure if any of the model's columns have changed, but you do want to update the updated_atregardless - this is the way to go.

这将正确保存updated_at到现在。如果您不确定模型的任何列是否已更改,但您确实想更新updated_at无论如何 - 这就是要走的路。