如何在 Laravel 迁移中设置默认 DateTime()?

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

How to set default DateTime() in Laravel migrations?

laravellaravel-migrations

提问by Alexander Lomia

I am trying to achieve the functionality illustrated below:

我正在尝试实现如下所示的功能:

$table->dateTime('time')->default(new \DateTime());

This exact code is failing, because

这个确切的代码失败了,因为

[ErrorException]
Object of class DateTime could not be converted to string

[ErrorException]
类 DateTime 的对象无法转换为字符串

The other problem is, that I feel that there should be more robust/elegant way of solving this issue. So, how to correctly set a default DateTimevalue in migrations?

另一个问题是,我觉得应该有更健壮/优雅的方法来解决这个问题。那么,如何DateTime在迁移中正确设置默认值呢?

回答by Gaurav Dave

Try this:

尝试这个:

$table->timestamp('time')->useCurrent = true;

See, if that helps.

看看,如果这有帮助。