Writing a SQL Query into Eloquent
I have a query as below, I am struggling to write it into Eloquent,
SELECT id, description FROM turnovers ORDER BY CASE WHEN id = (SELECT MAX(id) FROM turnovers) THEN 0 ELSE 1 END, id;
The model is Turnover.
Any help is appreciated.
You can use a raw query to achieve this! However Laravel also has a helper method called orderByRaw which works in your case
DB::table('turnovers')
->select('id', 'description')
->orderByRaw('CASE WHEN id = (SELECT MAX(id) FROM turnovers) THEN 0 ELSE 1 END, id');
Note: I didn't tested this query, but it should push you in the right direction ;)
Source: https://laraveldaily.com/know-orderbyraw-eloquent/
Please or to participate in this conversation.