douhan5853 2017-05-23 14:15
浏览 245

Laravel Eloquent - 如何按子数组值排序

Hello I have the following Eloquent query

$usersSessions = User::with('tracker_sessions')
                   ->whereHas('tracker_sessions', function($q){
                      $q->where('created_at','>=', Carbon::now()>subDays('7'));
                    })
                    ->with('tracker_geoip')->get()->all();

which returns the following structure :

draw: null,
  recordsTotal: 5,
  recordsFiltered: 5,
  data: [{
      id: 164,
      name: "Test User",
      first_name: "Test",
      last_name: "User",
      email: "test_user@gmail.com",
      tracker_sessions: [{
          id: 156,
          uuid: "abea7a1a-84ee-48ab-a2e9-35365e8f57e3",
          user_id: 164,
          device_id: 1,
          agent_id: 7,
          client_ip: "127.0.0.1",
          referer_id: 11,
          cookie_id: null,
          geoip_id: null,
          is_robot: 0,
          created_at: "2017-05-22 16:08:24",
          updated_at: "2017-05-22 09:08:24",
          language_id: 1
        },
        {
          id: 155,
          uuid: "61e27c18-4eb9-4d27-954a-836a4e9d1b04",
          user_id: 164,
          device_id: 1,
          agent_id: 7,
          client_ip: "127.0.0.1",
          referer_id: null,
          cookie_id: null,
          geoip_id: null,
          is_robot: 0,
          created_at: "2017-05-22 16:08:23",
          updated_at: "2017-05-22 09:08:23",
          language_id: 1
        }
      ],
      tracker_geoip: [{
        id: 1,
        country_code: "BG",
        country_code3: null,
        country_name: "Bulgaria",
        region: "EU",
        city: "Sofia",
        postal_code: "1000",
        area_code: null,
        dma_code: null,
        metro_code: null,
        continent_code: "EU",
        created_at: "2016-12-22 16:22:30",
        updated_at: "-0001-11-30 00:00:00",
        pivot: {
          user_id: 164,
          geoip_id: 1
        }
      }]

So the question is : “How can I sort the users by their latest tracker_sessions ?”

I tried to join the tracker_sessions table , but it didn't work . It’s not returning the tracker_sessions and geo_ips at all

$usersSessions = User::with('tracker_sessions')
                                ->whereHas('tracker_sessions', function($q){
                                    $q->where('created_at','>=', Carbon::now()->subDays('7'));
                                })
                                ->join('tracker_sessions', 'tracker_sessions.user_id', '=', 'users.id')
                                ->with('tracker_geoip');
</div>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度
    • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测