您好我有以下Eloquent查询
$usersSessions = User::with('tracker_sessions')
->whereHas('tracker_sessions', function($q){
$q->where('created_at','>=', Carbon::now()>subDays('7'));
})
->with('tracker_geoip')->get()->all();
返回以下结构:
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
}
}]
所以问题是:“我如何按最新的tracker_sessions对用户进行排序?”
我尝试加入tracker_sessions表,但它没有用。 它并没有返回tracker_sessions和geo_ips
$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');