Support the ongoing development of Laravel.io →
posted 1 year ago
Last updated 1 year ago.
0
moderator Solution

Hello @jtsimon93

You check the result from sort and sortByDesc. The reverse version of sort is sortDesc. While sortByDesc is related to sortBy.

From the docs: https://laravel.com/docs/10.x/collections#method-sortby

The sortBy method sorts the collection by the given key. The sorted collection keeps the original array keys,

And because of the keys the result will be on the way you did found out.

Another point I see is that you do the sorting on the collection (the sortBy after the get) it is also possible to let your database handle it, this will result in a performance improvement.

You can do it with:

$memberResult = Member::with('addresses', 'phones', 'emails', 'emergencyContacts')
    ->orderBy('last_name')
    ->get();

// reverse
$memberResult = Member::with('addresses', 'phones', 'emails', 'emergencyContacts')
    ->orderByDesc('last_name')
    ->get();

jtsimon93 liked this reply

1
Solution selected by @jtsimon93

Thank you so much @tvbeek! That was very helpful!!

0

Sign in to participate in this thread!

Eventy

Your banner here too?

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.