我正在尝试这个简单的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\")'
答案 0 :(得分:2)
好的,我只是添加了DB :: raw,它有效:)
$tbl->select( 'DB::raw('DATE_FORMAT(my_date, \'%m-%d-%Y\') AS arrival')' );