Flor Van de Kerckhove
Flor Van de Kerckhove

Reputation: 55

Mysql sum group by query in laravel eloquent

I am trying to convert a query to eloquent or find a way to use it in a laravel controller.

The query is:

select employee, workdate, sum(actualhours) 
from ts_data group by employee, workdate;"

Upvotes: 1

Views: 1082

Answers (1)

Mihir Bhende
Mihir Bhende

Reputation: 9055

Using Eloquent Model :

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

or using DB Facade :

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

Upvotes: 2

Related Questions