Level 47
Why if it's 90% raw query anyways?
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I'm trying to write:
SELECT a.id FROM posts a INNER JOIN ( SELECT DATE(timestamp) date, MAX(likes) max_likes FROM posts GROUP BY DATE(timestamp)) b ON DATE(a.timestamp) = b.date AND a.likes = b.max_likes
The inner join is simple, it's:
$inner = DB::table('posts as b')
->selectRaw('date(timestamp) date')
->groupBy(DB::raw('date(timestamp)'));
I'm just not sure how to get started on the outer...
$outer = DB::table('posts as a')
->selectRaw('a.id')
->join($inner)
->whereRaw(['DATE(a.timestamp) = b.date'], ['a.likes = b.max_likes']);
Please or to participate in this conversation.