Laravel4.2:Eloquent - 在SQL查询中使用DATE_FORMAT

时间:2015-03-03 12:22:48

标签: laravel-4 eloquent

我正在尝试这个简单的SQL查询

SELECT DATE_FORMAT(my_date, '%m-%d-%Y') FROM my_tbl WHERE id = 1

在Eloquent中

$tbl = DB::table('my_tbl');
$tbl->select( 'DATE_FORMAT(my_date, "%m-%d,-%Y")' );
$tbl->where( 'id', "=", 1);
Datatable::query($tbl);

但它接受字符串并抛出错误:

{"error":{"type":"Illuminate\\Database\\QueryException","message":"SQLSTATE[42S22]: Column not found: 1054 Unknown column 'DATE_FORMAT(arrival, \"%d %M, %Y\")'

1 个答案:

答案 0 :(得分:2)

好的,我只是添加了DB :: raw,它有效:)

$tbl->select( 'DB::raw('DATE_FORMAT(my_date, \'%m-%d-%Y\') AS arrival')' );