Eloquent更新方法更改created_at时间戳 [英] Eloquent update method change created_at timestamp

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

问题描述

我使用Eloquent进行数据库处理,当我使用更新方法更新记录 created_at 时更改了如何防止这种情况发生?

I'm using Eloquent for database handling, when I update a record with update method created_at time stamp is changed how to prevent this from happening ?

$post = Post::update([
   'name' => $name
]);

updated_at >

推荐答案

如果模型中没有任何自定义设置。您可以更新这样的记录:

In case you don't have any custom settings in your model. You can update record like this:

$id = 5; // here put id of record you want to update
$post = Post::findOrFail($id);
$post->name = $name;
$post->save();

仅使用此代码 name updated_at 时间戳(而不是 created_at )。如果 created_at 也更新,则表示您可能有一些自定义模型设置或:

Using this code only name should be updated and updated_at timestamp (and not created_at). If created_at is also updated it means you might have some custom model settings or:


  • 您可能有数据库触发器在更新记录时更改 created_at

  • 您可能已注册了一些更改 created_at c> c> 未定义为 CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP - 无论何时更新记录, created_at 也将更新为当前的datetime

  • you might have database triggers that change created_at when record is updated
  • you might have registered some events that change created_at when record is updated
  • make sure your created_at field in posts table in database is not defined as CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP - this way whenever you update record, created_at will be also updated to current datetime

您还应确保您没有 Post 模型类似这样:

You should also make sure that you don't have for your Post model something like this:

const UPDATED_AT = 'created_at';

如果也会解释为什么 created_at 当您更新记录时。

If would also explain why created_at is changed when you update your record.

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

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