Php 将查询转换为Laravel

Php 将查询转换为Laravel,php,sql,laravel,Php,Sql,Laravel,我从中得到这个查询,有人能把它转换成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_v

我从中得到这个查询,有人能把它转换成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)
这是错误消息

$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)

谢谢

删除多余的括号并使用
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);

为什么否决我的noob问题(尝试删除括号?问题似乎是Elount希望排序顺序(asc,desc)作为orderBy方法的第二个参数传递,就像这样
orderBy('created_at','desc');
。它起作用了!我正在删除额外的括号,现在我在合并orderBy(DB::raw('FIELD(position,1,2)'),'asc'时遇到了问题)这感谢我使用$position=position::orderByRaw('CASE WHEN position\u value=1然后在asc末尾创建,CASE WHEN position\u value=2然后在末尾创建')->paginate(29);我删除了最后一个'desc',因为它有自己的默认值。如何以正确的方式进行注释,语法从不突出显示