好的,经过几个小时的研究,仍然使用DB::select,我不得不问这个问题.因为我要把电脑扔掉;).
我想获取用户的最后一次输入(基于时间戳).我可以使用原始SQL做到这一点
SELECT c.*, p.*
FROM users c INNER JOIN
(
SELECT user_id,
MAX(created_at) MaxDate
FROM `catch-text`
GROUP BY user_id
) MaxDates ON c.id = MaxDates.user_id INNER JOIN
`catch-text` p ON MaxDates.user_id = p.user_id
AND MaxDates.MaxDate = p.created_at
I got this query from another post here on stackoverflow.
I have tried everything to do this with the fluent query builder in Laravel however with no success.
I know the manual says you can do this:
DB::table('users')
->join('contacts', function($join)
{
$join->on('users.id', '=', 'contacts.user_id')->orOn(...);
})
->get();
But that is not helping much because I do not see how I could use a subquery there? Anyone who can light up my day?