如何在 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
How to set default DateTime() in Laravel 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 DateTime
value in migrations?
另一个问题是,我觉得应该有更健壮/优雅的方法来解决这个问题。那么,如何DateTime
在迁移中正确设置默认值呢?
回答by Gaurav Dave
Try this:
尝试这个:
$table->timestamp('time')->useCurrent = true;
See, if that helps.
看看,如果这有帮助。