Support the ongoing development of Laravel.io →
Eloquent Views Blade
Last updated 2 years ago.
0

First of all i don't get it why u don't want to do the order in the select query, in my opinion it is more neat and has a better performance then sorting in php. But since you don't want that, well i see two ways : 1- You sort your collection based on the id value : may be this could help http://www.mabishu.com/blog/2011/02/03/sort-an-array-of-objects-by-one-of-the-object-property-with-php/ but in your case you will be iterating a collection of objects and not an array of objects.

2- Use the toArray() method to transform everything to array and the you can use the array sort functions http://php.net/manual/fr/ref.array.php

In the end as you can see doing the sorting in php is a mess compared to doing it in sql ORDER BY id ASC (or the equivalent in Query Builder order() method)

Last updated 2 years ago.
0

Or simply:

<h1>{{ $post->title }}</h1>
@foreach($post->comments->reverse() as $comment)
    {{ $comment->title }}
@endforeach
Last updated 2 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

tzookb tzookb Joined 9 Feb 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.