php Laravel 时间戳()不会创建 CURRENT_TIMESTAMP

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

Laravel timestamps() doesn't create CURRENT_TIMESTAMP

phplaraveleloquentlaravel-migrationslaravel-seeding

提问by Get Off My Lawn

I have a migration that has the timestamps()method, and then I have a seed to seed this table.

我有一个具有该timestamps()方法的迁移,然后我有一个种子来为这个表播种。

Schema::create('mytable', function (Blueprint $table) {
    $table->increments('id');
    $table->string('title');
    $table->timestamps();
});

The seed looks like this:

种子看起来像这样:

DB::table('mytable')->insert([
    [
        'title' => 'My Awesome Title'
    ]
]);

When it all gets run using:

当它全部运行时:

php artisan migrate:refresh --seed

The item gets inserted, but the values of created_atand updated_atare both 0000-00-00 00:00:00why are they not set correctly?

该项目被插入,但值created_atupdated_at0000-00-00 00:00:00那他们为什么不正确?

here are the column schemes that it creates:

这是它创建的列方案:

`created_at` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00',

I would like these schemes:

我想要这些方案:

`created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

回答by Marcin Nabia?ek

When you insert data not using Eloquent you need to insert timestamps on your own.

当您不使用 Eloquent 插入数据时,您需要自己插入时间戳。

If you use:

如果您使用:

$x = new MyTable();
$x->title = 'My Awesome Title';
$x->save();

you will have timestamp filled correctly (of course you need to create MyTablemodel first)

您将正确填写时间戳(当然您需要先创建MyTable模型)

EDIT

编辑

If you really want it you can change:

如果你真的想要它,你可以改变:

$table->timestamps();

into:

进入:

$table->timestamp('created_at')->default(\DB::raw('CURRENT_TIMESTAMP'));
$table->timestamp('updated_at')->default(\DB::raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP'));

And if you create model for this table, you should set

如果你为此表创建模型,你应该设置

$timestamps = false;

to make sure Eloquent won't try to set them on his way.

以确保 Eloquent 不会试图让他们上路。

EDIT2

编辑2

There is also one more important issue. In case you mix setting dates in tables from PHP and in other in MySQL you should make sure that both in both PHP and MySQL there's exact same datetime (and timezone) or you should use the same date comparison as you set in record (either MySQL or PHP). Otherwise when running queries you might get unexpected results for example

还有一个更重要的问题。如果您在 PHP 和 MySQL 中的其他表中混合设置日期,您应该确保在 PHP 和 MySQL 中都有完全相同的日期时间(和时区),或者您应该使用与您在记录中设置的相同日期比较(MySQL或 PHP)。否则在运行查询时,您可能会得到意想不到的结果,例如

SELECT * FROM mytable WHERE DATE(created_at) = CURDATE()

might be different than running query with passing PHP date

可能与通过 PHP 日期运行查询不同

"SELECT * FROM mytable WHERE DATE(created_at) = '".date('Y-m-d")."'"

because on PHP server it might be for example 2015-12-29 but on MySQL server 2015-12-30

因为在 PHP 服务器上它可能是例如 2015-12-29 但在 MySQL 服务器上它可能是 2015-12-30

回答by bmatovu

For later versions, you can simply use. (source)

对于以后的版本,您可以简单地使用。(来源

$table->timestamp('created_at')->useCurrent();
$table->timestamp('updated_at')->useCurrent();

回答by Sari Yono

i would use the carbon library if i am seeding in the timestamps and set that up in the factory. if not you could do something like this :

如果我在时间戳中播种并在工厂中进行设置,我会使用 carbon 库。如果不是,你可以做这样的事情:

$timestamps = false;

and i would remove the the $table->timestamps();from the migration if i am not going to use it.

$table->timestamps();如果我不打算使用它,我会从迁移中删除 它。