Laravel Eloquent: Ordering results of all()

MatterGoal picture MatterGoal · Jul 2, 2013 · Viewed 212.3k times · Source

I'm stuck on a simple task. I just need to order results coming from this call

$results = Project::all();

Where Project is a model. I've tried this

$results = Project::all()->orderBy("name");

But it didn't work. Which is the better way to obtain all data from a table and get them ordered?

Answer

Travis B picture Travis B · Jul 2, 2013

You can actually do this within the query.

$results = Project::orderBy('name')->get();

This will return all results with the proper order.