I'm stuck at understanding something melted between Laravel Query Builder and pure SQL I guess.
I have 2 tables :
- User : containing users with primary key
user_id - User_Action : which is a table containing records with composed PRIMARY KEYs of the
user_idand thedatetimeof the action.
ex:
user
user_id
1
user_action
user_id datetime action_type
1 2017-12-01 12:10:00 y
1 2017-12-01 12:00:00 x
My need :
Now I want to retrieve a user list, with the newest action for each user. So I need to get only 1 row in the JOIN from user to user_action, and this row have to be the one with newest datetime.
So in my ex; I would like to get only the record with the datetime 2017-12-01 12:10:00 with a LEFT JOIN (to retrieve user even if there's no action).
I tried the following :
$userActionSubquery = DB::table('user_action')
->select()
->orderBy('datetime', 'DESC')
->limit(1);
$query->leftJoinSub($userActionSubquery, 'user_action', function ($join) {
$join->on('user_action.user_id', '=', 'user.user_id');
})->groupBy('user_id');
By doing so, I'm not getting anything from user_action table, BUT it works if the action of the given user is the newest of the table !
I thought the $join->on would filter user id but the JOIN subquery is ran without this filter, and I can't but a WHERE in subquery because I don't have a user_id to give ! it's a query to get a list of user !
PS : the query generated is
SELECT user.user_id FROM user left join (SELECT * FROM user_action ORDER BY datetime DESC LIMIT 1) as user_action on user_action.user_id = user.user_id WHERE user.tenant_id in ('7') GROUP BY `user_id`
I'm missing something to succeed, thanks for your help!