Laravel - Order by as Number (int), even if column type is string

Viewed 7600

here it is:-

$query = Section::orderBy("section", "desc")->get();

section here is a column with type string yet it is having numbers in it where i want to order by those numbers thanks for your response

3 Answers

First you should use proper data types while designing schema.

For your existing schema you can tweak your order by clause to type cast your value at runtime using orderByRaw method

->orderByRaw('section * 1 desc') 
$query->orderByRaw("section::int", "desc")->get();
Related