Laravel orderBy not correctly ordering

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP

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?





Can you let us know what is meant by @if($loop->iteration == 1) and @if($loop->iteration == 2) ?
– vivek_23
4 hours ago



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

Comments

Popular posts from this blog

Executable numpy error

PySpark count values by condition

Mass disable jenkins jobs