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
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community