更新 Created_at 时间戳 Laravel 数据库播种

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24903637/
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-09-14 09:50:57  来源:igfitidea点击:

Update Created_at timestamp Laravel db seeding

phplaravellaravel-4

提问by user3413723

I'm running an app that processes user data entered over a period of time. For that, I need to change the timestamps in the data I use to seed the database. I have a nice loop to do it, but the problem is, when I run the database seeding, nothing happens. I'm wondering if there is a Laravel specific thing that prevents this, and if so how to get around it.

我正在运行一个应用程序来处理在一段时间内输入的用户数据。为此,我需要更改用于为数据库设定种子的数据中的时间戳。我有一个很好的循环来做到这一点,但问题是,当我运行数据库播种时,没有任何反应。我想知道是否有 Laravel 特定的东西可以防止这种情况发生,如果是,如何解决它。

            $record->created_at = $fakeCreateDate->format('Y-m-d H:i:s');
            $record->save();

采纳答案by Christopher Pecoraro

Yes, created_at and updated_at are "special". Laravel sets the timestamp for created_at at create time and updated_at at update time.

是的, created_at 和 updated_at 是“特殊的”。Laravel 在创建时为 created_at 设置时间戳,在更新时为 updated_at 设置时间戳。

http://laravel.com/docs/eloquent#timestamps

http://laravel.com/docs/eloquent#timestamps

You can turn them off by doing this inside your model

您可以通过在模型中执行此操作来关闭它们

public $timestamps = false;

By default, Eloquent will maintain the created_at and updated_at columns on your database table automatically. Simply add these timestamp columns to your table and Eloquent will take care of the rest. If you do not wish for Eloquent to maintain these columns, add the following property to your model

Once a model is defined, you are ready to start retrieving and creating records in your table. Note that you will need to place updated_at and created_at columns on your table by default. If you do not wish to have these columns automatically maintained, set the $timestamps property on your model to false.

默认情况下,Eloquent 会自动维护数据库表上的 created_at 和 updated_at 列。只需将这些时间戳列添加到您的表中,Eloquent 将处理其余的工作。如果您不希望 Eloquent 维护这些列,请将以下属性添加到您的模型中

定义模型后,您就可以开始在表中检索和创建记录了。请注意,默认情况下您需要在表中放置 updated_at 和 created_at 列。如果您不希望自动维护这些列,请将模型上的 $timestamps 属性设置为 false。

回答by user985366

The Carbon::now()method can help you here.

Carbon::now()方法可以帮助您。

In your example, it should work with

在您的示例中,它应该与

$record->created_at = \Carbon\Carbon::now()
$record->save()

If you need to do an insert with DB::table, for example to update the date, you need to use the right method to create the date.

如果您需要使用 进行插入DB::table,例如更新日期,则需要使用正确的方法来创建日期。

Let's say you have a table thingswith only idand timestampslike this Schema

比方说,你有一个表things,只有idtimestamps这样的架构

$table->increments('id');
$table->timestamps();

One example of a date update is:

日期更新的一个示例是:

DB::table('things')->insert(['created_at' => \Carbon\Carbon::now()]);