Mysql 使用agregate方法将SQL转换为elount

Mysql 使用agregate方法将SQL转换为elount,mysql,laravel,eloquent,Mysql,Laravel,Eloquent,如何将该SQL查询转换为Laravel 4中的雄辩模式: SELECT count(distinct(worker_id)) FROM formation_worker WHERE formation_id in(SELECT id FROM formations WHERE YEAR(start_date)=YEAR(now())) 关于,这是我基于@limonte回复的解决方案: FormationWorker::select(DB::raw('count(distinct(worker_

如何将该SQL查询转换为Laravel 4中的雄辩模式:

SELECT count(distinct(worker_id)) FROM formation_worker WHERE formation_id in(SELECT id FROM formations WHERE YEAR(start_date)=YEAR(now()))

关于,

这是我基于@limonte回复的解决方案:

FormationWorker::select(DB::raw('count(distinct(worker_id))'))
    ->whereIn('formation', function($sq) {
        $sq->select('id')
           ->from('formations')
           ->whereRaw('YEAR(start_date)=YEAR(now())');
    });
DB::table('formation_worker')
                            ->select(DB::raw('count(distinct(worker_id))'))
                                ->whereIn('formation_id', function($sq) {
                                    $sq->select('id')
                                       ->from('formations')
                                       ->whereRaw('YEAR(start_date)=YEAR(now())');
                                })->first()

谢谢你的快速回复。但就我而言,我没有数据透视表的模型。我应该创建它还是有另一种选择??您要求的是雄辩的解决方案,这是解决方案:)