Как реализовать сложный запрос в eloquent?
$query = Ad::
select(array('ads.*', DB::raw('COUNT(DISTINCT clicks.id) as clicks_count'), DB::raw('COUNT(DISTINCT shows.id) as shows_count'), DB::raw('(COUNT(DISTINCT clicks.id) * COUNT(DISTINCT shows.id))/100 as CTR')))
->leftJoin('clicks', function($join) use($date){
	$join->on('ads.id', '=', 'clicks.ad_id')->where(DB::raw('DATE(clicks.created_at)'), '=', $date);
})
->leftJoin('shows', function($join) use($date){
	$join->on('ads.id', '=', 'shows.ad_id')->where(DB::raw('DATE(shows.created_at)'), '=', $date);
})
->groupBy('ads.id')->with('devices', 'platforms');
Подсчитывает просмотры, клики, цтр и возвращает инфу по платформам, девайсам.
SELECT ads.*, COUNT(DISTINCT clicks.id) as clicks, COUNT(DISTINCT shows.id) as shows, (COUNT(DISTINCT clicks.id) * COUNT(DISTINCT shows.id))/100 as CTR
FROM ads
LEFT JOIN shows ON ads.id = shows.ad_id
LEFT JOIN clicks ON ads.id = clicks.ad_id AND shows.created_at = clicks.created_at
WHERE shows.created_at = '2014-02-24'
GROUP BY ads.id
Я бы делал так. А в чем проблема в ларавеле? как пробовали?
AND в втором JOIN можно убрать в WHERE