2015-03-03 39 views
1

我想這個簡單的SQL查詢Laravel4.2:口才 - 使用DATE_FORMAT在SQL查詢

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

雄辯像

$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\")' 

回答

2

好吧我只是添加DB :: raw,它工作:)

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