0

I want to sort a collection based on two criteria. In my project I have an ideas table. Ideas belongs to a campaign, and also have a numeric status. So, for one of my reports I want to sort first for campaign, and then by status desc.

If it were SQL it would be like

SELECT * FROM IDEAS
ORDER BY campaign_id, status DESC

Now with the ORM I do

$ideas = idea()->sortBy('campaign_id')->get()

So, what is the correct way to get the same result as the SQL statement above?

(First I read this answer How to sort Laravel Eloquent ORM objects? but it only refers to one key)

luisfer
  • 1,927
  • 7
  • 40
  • 54
  • @Stuart I could be mistaken, but I don't think this is asking the same thing. My question that you linked is asking about sorting the collection after the query result has already been fetched. This appears to be asking how to add another sort criteria to the query. (Although the title does say collections, so not sure...) – Don't Panic Dec 17 '18 at 22:05
  • @luisfer, if that is the case, check out the second answer on the question you linked. ;-) – Don't Panic Dec 17 '18 at 22:11
  • Duh! How dumb. I read the accepted response and didn't worked for me, so I didn't scrolled more, yeah, the correct answer is there. What do I do with this question? should I delete it? – luisfer Dec 18 '18 at 00:05
  • 1
    Nah, duplicates are not necessarily bad. They can potentially help people find their way to the answer they're looking for if their search matches your question better. – Don't Panic Dec 18 '18 at 00:16

0 Answers0