Laravel Eloquent: Ordering results of all()
In addition, just to buttress the former answers, it could be sorted as well either in descending desc
or ascending asc
orders by adding either as the second parameter.
$results = Project::orderBy('created_at', 'desc')->get();
You could still use sortBy (at the collection level) instead of orderBy (at the query level) if you still want to use all() since it returns a collection of objects.
Ascending Order
$results = Project::all()->sortBy("name");
Descending Order
$results = Project::all()->sortByDesc("name");
Check out the documentation about Collections for more details.
https://laravel.com/docs/5.1/collections
You can actually do this within the query.
$results = Project::orderBy('name')->get();
This will return all results with the proper order.