doumi9618 2018-11-08 21:07
浏览 9

仅获取列数最高的用户

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?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 公交车和无人机协同运输
    • ¥15 stm32代码移植没反应
    • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
    • ¥100 连续两帧图像高速减法
    • ¥15 组策略中的计算机配置策略无法下发
    • ¥15 如何绘制动力学系统的相图
    • ¥15 对接wps接口实现获取元数据
    • ¥20 给自己本科IT专业毕业的妹m找个实习工作
    • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
    • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)