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

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 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

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.

Or simply:

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

Sign in to participate in this thread!


Your banner here too?

tzookb tzookb Joined 9 Feb 2014


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

Your logo here?

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

© 2024 - All rights reserved.