Skip to content
Advertisement

Laravel Multiple Sortby Not Working Properly

i have some issues with laravel sortBy ( laravel 5.4 ) .. based on what i read on many websites, it says that for doing multiple sortBy laravel was by using reverse order.. so i try to do that.. but still not working properly..

So here it is.. I have this collection of object…

JavaScript

My Goal is to have this objects sorted following this order

score ( asc ) > int_premi ( asc ) > rates ( desc ) > promo ( as boolean ) ( desc ) > views ( desc ) > product_id ( desc )

So i write this code..

JavaScript

I’m looking for the result comes with this order

BCD > DEF > ABC

Instead, not following that order..

So is there anyone also facing the same issue with me ? and maybe someone can help me out through this issue ?

Thankyou Very much

Advertisement

Answer

After doing some research.. i found the one is working.. If you are facing the same thing.. this article might help..

https://www.jjanusch.com/2017/05/laravel-collection-macros-adding-a-sortbymuti-function

So my final is exact same thing with this article suggestion, by creating a macro.. like this

JavaScript

Then you can just using it on your collection like this

JavaScript

prop_one and prop_two is your collection properties.. Hope this help

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement