Tuesday, May 23, 2017

Laravel Eloquent - how to sort by sub array values

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 .



via user3102290

Advertisement