Laravel orderBy not correctly ordering
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgKPz5qhCkKGdpPJ0wkSm5JqlliSJoZg92-H4DuCruovhkVFuXerCphZt9wys_Gr3uwQyXObMHDwattjG5BSeCEl_LdDq1GPLct8qYyRyX4fZ9C93oW-TX7IGZLYDwqRgfFCXA4aUxkBAcS/s1600/1.jpg)
![Creative The name of the picture](https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh6VebLk98DYwS3K2nWZSCatab-q4HrSuRk91M4fQA8SHrKU_-x-lq1n780pSU_bIVLqMRv6pc3dhhh-Y2PUwBS9mM0BxvY1eM6XsM-e7Diu4z-wAoeDG0vB3-2_1U9RE8sO0lg4PHiIXbI/s400/Clash+Royale+CLAN+TAG%2523URR8PPP.png)
Laravel orderBy not correctly ordering
I have a table called users
and I have row called ELO_points
.
users
ELO_points
Some users have "-10" ELO_points and some have "25".
Sometimes orderBy
not working correctly.
orderBy
My controller:
$users = User::orderBy('ELO_points', 'DESC')->take(5)->get();
My view:
@foreach ($users as $user)
@if($loop->iteration == 1)
<div class="right-points">!! $user->ELO_points !!</div>
</li>
@endif
@if($loop->iteration == 2)
<div class="right-points">!! $user->ELO_points !!</div>
</li>
@endif
@endforeach
Any help why my ordering not showing like normal from -10 to 10?
@if($loop->iteration == 1)
@if($loop->iteration == 2)
Don't you have those values saved as the strings instead of the signed integers?
– Dawid Zbiński
3 hours ago
@vivek_23 i taking first of foreach and second by using
@if($loop->iteration == 1)
That becouse i need custom text for first foreach item :)– Mynde Mindaugelis
3 hours ago
@if($loop->iteration == 1)
@MyndeMindaugelis Best way to debug this is to print
$users
after the eloqunet query itself and show us what you get(by editing your post). Like Dawid said, are the values stored as integers or is it varchar in the DB table?– vivek_23
2 hours ago
$users
Any help why my ordering not showing like normal from -10 to 10?
, you're using DESC
so it should go from 10 to -10...shouldn't it?– adolfotcar
1 hour ago
Any help why my ordering not showing like normal from -10 to 10?
DESC
1 Answer
1
Try it with the following query.
$query = "CAST(ELO_points AS INT) DESC";
$users = User::orderByRaw($query)->take(5)->get();
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Can you let us know what is meant by
@if($loop->iteration == 1)
and@if($loop->iteration == 2)
?– vivek_23
4 hours ago