[英]Laravel 5.5 - Eloquent - Retrieve “microseconds” from MySQL table with “datetime(6)” field
我从MySQL 5.7表中检索datetime(6)
字段时遇到问题。 如果我直接从mysql客户端运行它,它将起作用:
mysql> SELECT arrival from table1 limit 1;
+----------------------------+
| arrival |
+----------------------------+
| 2016-06-22 16:52:06.260000 |
+----------------------------+
1 row in set (0.00 sec)
mysql>
但是使用Eloquent从Laravel获取字段时,不会报告微秒:
class Table1Model extends Model
{
protected $table = 'table1';
}
class Table1Controller extends Controller
{
public function index()
{
$data = Table1Model::first()->arrival;
dd($data);
}
}
// the output is: "2016-06-22 16:52:06"
这是口才吗? 如何获得微秒?
谢谢。
谢谢@apokryfos,是的。。。这是一个错误。
我发现了使用RAW
查询的简单解决方法:
class Table1Model extends Model
{
protected $table = 'table1';
}
class Table1Controller extends Controller
{
public function index()
{
$query = Table1Model::select(
DB::raw(
CONVERT(CAST(arrival AS DATETIME(3)), CHAR)
)
);
$data = $query->get();
dd($data);
}
}
// the output is: "2016-06-22 16:52:06.260"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.