使用聚合方法将 SQL 转换为 Eloquent
Convert SQL to Eloquent with agregate methods
如何将 SQL 查询转换为 Eloquent 模式 In Laravel 4 :
SELECT count(distinct(worker_id)) FROM formation_worker WHERE formation_id in(SELECT id FROM formations WHERE YEAR(start_date)=YEAR(now()))
此致,
FormationWorker::select(DB::raw('count(distinct(worker_id))'))
->whereIn('formation', function($sq) {
$sq->select('id')
->from('formations')
->whereRaw('YEAR(start_date)=YEAR(now())');
});
这是我基于@limonte 回复的解决方案:
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()
如何将 SQL 查询转换为 Eloquent 模式 In Laravel 4 :
SELECT count(distinct(worker_id)) FROM formation_worker WHERE formation_id in(SELECT id FROM formations WHERE YEAR(start_date)=YEAR(now()))
此致,
FormationWorker::select(DB::raw('count(distinct(worker_id))'))
->whereIn('formation', function($sq) {
$sq->select('id')
->from('formations')
->whereRaw('YEAR(start_date)=YEAR(now())');
});
这是我基于@limonte 回复的解决方案:
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()