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的文件

同样,UPDATED_AT时间戳将自动更新,因此无需手动设置其值

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

它只在第一次插入时设置,但不在更新时设置.当我手动操作时,就像这样

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

它起作用了.然而,doctor 说这应该会自动发生,那么这里出了什么问题?

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

How would I do that correctly?

推荐答案

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

Laravel相关问答推荐

如何更改LIVE上的Filament占位符内容?

到查询构建器的MySQL查询

laravel vue 惯性分页器删除上一个和下一个链接

Laravel 9 如何将 url 中的参数从一个控制器传递到另一个控制器?

使用 laravel 根据当月显示注册用户列表

laravel 关系集合上的自定义排序

Laravel 错误ReflectionException-类 App\Http\Kernel 不存在

Laravel 4:如何将 WHERE 条件应用于 Eloquent 类的所有查询?

PHP Laravel:如何设置或获取会话数据?

你如何找到 Laravel 外观背后的底层类?

Laravel 4 定义 RESTful 控制器

使用 ModelNotFoundException

如何在中间件 Laravel 中获取请求的控制器和操作的名称

Eloquent 的 Model::query() 是什么意思?

Laravel/Lumen 中两列之间的 SQL

Laravel - 语法错误,文件意外结束

Laravel 5 事件处理程序未触发

如何使 Laravel (Blade) 文本字段只读

Laravel 更新后用户模型错误(用户类包含 3 个抽象方法)

Laravel:Redis 无法建立连接:[tcp://127.0.0.1:6379]