I have an attribute in the database that I want to use a filter on by sorting it as ASC or DESC. I want the order of the elements to be
highest
high
medium
low
lowest
Using the ASC $q->orderBy('priority', 'ASC'); the result given is
high
highest
low
lowest
medium
What can I do to sort the elements in that order? thanks
>Solution :
You can use a case statement in order by:
order by
case column_name
when 'highest' then 5
when 'high' then 4
when 'medium' then 3
when 'low' then 2
when 'lowest' then 1
end desc
So something like this should work:
$q->orderByRaw("case column_name
when 'highest' then 5
when 'high' then 4
when 'medium' then 3
when 'low' then 2
when 'lowest' then 1
end desc");