I have a users
table and a score
table. In my score
table there is a field called 'points'
. I want to only select the user that has the highest points. So user with the highest number in my score
table.
Currently with my code I'm only getting the first user. For example I have 2 users, John and Doe. John has 70 points and Doe has 80 points. So I want Doe to be logged in the console, but instead John is being logged in the console. What am I missing here?
Code
Date::All()
->each(function($date, $key) {
if($date->period->isToday()) {
$users = User::with('score')->get();
$maxUser = $users->sortByDesc('points')->first();
\Log::debug($maxUser);
}
});
Output
{"id":1,"name":"John Doe","username":"John 123","country":"Belgium","email":"John@example.com","email_verified_at":null,"type":"default","participated":0,"created_at":"2018-11-08 20:23:47","updated_at":"2018-11-08 20:23:47","deleted_at":null,"score":{"id":1,"user_id":1,"points":70,"created_at":"2018-11-08 20:23:47","updated_at":"2018-11-08 20:23:47"}}
I want my maxUser
to be Doe instead of John since he got the highest points, any tips?