将查询转换为Laravel

时间:2014-07-04 02:45:09

标签: php sql laravel

我从Pragnesh Khalas收到此查询,有人可以将其转换为laravel查询吗?

SELECT * FROM Position ORDER BY 
CASE  WHEN position_value = 1 THEN created_at END ASC,
CASE WHEN position_value = 2 THEN created_at END DESC

我已经这样做但仍然收到错误消息

$position = Position::orderBy(DB::raw('(CASE WHEN position_value = 1 THEN created_at END asc, CASE WHEN position_value = 2 THEN created_at END desc)'))->paginate(29);

这是错误消息

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'asc, CASE WHEN position_value = 2 THEN created_at END desc) asc limit 29 offset 0' at line 1 (SQL: select * from `position` order by (CASE WHEN position_value = 1 THEN created_at END asc, CASE WHEN position_value = 2 THEN created_at END desc) asc limit 29 offset 0)

感谢

1 个答案:

答案 0 :(得分:2)

删除额外的括号并使用orderByRaw方法

$position = Position::orderByRaw('CASE WHEN position_value = 1 THEN created_at END asc, CASE WHEN position_value = 2 THEN created_at END desc')
           ->paginate(29);