Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AucT's avatar
Level 2

Custom OrderBy in Laravel Query Builder

Hi! I'm making a bit custom orderBy, how can I get rid of second parameter? it always sets desc when I put any other value (false, null, empty string).

p.s. if you wonder what I'm doing - I need to order by whereIn (so need something like order by id(5,3,7,1,6,12,8) and yes, I do need eloquent for eager loading and other awesome stuff it gives.

0 likes
3 replies
willvincent's avatar
Level 54

use orderByRaw() instead of the normal orderBy() method.

Then for your specific example, I believe you could do:

->orderByRaw('FIELD(id,5,3,7,1,6,12,8)')
12 likes

Please or to participate in this conversation.