In one of my recent project I needed to extract the last 8 users that have submitted a post.
After some tenativse I found this quite simple solution using the power of Eloquent.
DB::table('posts')
->join('users', function ($join) {
$join->on('posts.id', '=', DB::raw(
"( SELECT pi.id
FROM posts AS pi
WHERE pi.user_id = users.id
ORDER BY pi.created_at DESC
LIMIT 1
)"
));
})
->orderBy('posts.id','DESC)
->take(8)
->get();