通过laravel eloquent查询Mysql sum group

问题描述 投票:0回答:1

我试图将查询转换为雄辩或找到一种方法在laravel控制器中使用它。

查询是:

select employee, workdate, sum(actualhours) 
from ts_data group by employee, workdate;"
mysql laravel eloquent
1个回答
1
投票

使用雄辩模型:

$records = TsData::select(
    'employee',
    'workdate',
    \DB::raw('sum(actualhours) as sumhours')
)
->groupBy('employee', 'workdate')
->get();

或使用DB Facade:

$records = \DB::table('ts_data')->select(
    'employee',
    'workdate',
    \DB::raw('sum(actualhours) as sumhours')
)
->groupBy('employee', 'workdate')
->get();
© www.soinside.com 2019 - 2024. All rights reserved.