你不能,因为PHP PDO驱动程序不支持时间戳中的小数秒.解决方法是选择时间戳作为字符串,这样PDO驱动程序就不会知道它真正的时间戳,只需要执行$query-> selectRaw(DB :: raw(“CONCAT(my_date_column)as my_date_column”))但这意味着您不能对所有字段使用默认选择,因此查询会变得非常痛苦.您还需要在模型上覆盖其他几个与时间戳相关的方法.
// override to include micro seconds when dates are put into mysql.
protected function getDateFormat()
{
return 'Y-m-d H:i:s.u';
}
// override to include micro seconds in the created Carbon.
public function freshTimestamp()
{
$microtime = microtime(true);
$milliseconds = sprintf("%03d", ($microtime - floor($microtime)) * 1000);
return date('Y-m-d H:i:s.'. $milliseconds, $microtime)."";
}
最后在你的迁移而不是nullableTimestamps中,在Schema回调之外做:
DB::statement("ALTER TABLE `$tableName` ADD COLUMN created_at TIMESTAMP(3) NULL");
请注意,此示例适用于3个小数位,但如果您愿意,最多可以有6个,通过将两个位置中的3更改为6,在alter table和sprintf中,并将乘数* 1000调整为1000000为6.
PHP最近确实显示了它的年龄,我认为这个问题是我考虑转向更现代的Node.js的原因之一.