Domantas Šlaičiūnas
Domantas Šlaičiūnas

Reputation: 369

Convert Mysql query to Laravel elequent

I trying to convert MySQL query to Laravel eloquent, but not getting the right output.

MySQL query -

Select contents.con_type, contents.operation, contents.ecu, contents.ecu_sub, COUNT(*)
    from contents 
    group by operation, con_type, ecu, ecu_sub 
    order by operation asc, con_type asc

MySQL query to Laravel elequent -

$content = Content::query();
if ($request->input('total') == '1') {
$content = $content->select('con_type', 'operation', 'ecu', 'ecu_sub')
                   ->groupBy('operation', 'con_type', 'ecu', 'ecu_sub')
                   ->orderBy('operation', 'asc')
                   ->orderBy('con_type', 'asc')
                   ->get()->count();
}

The output I trying to get:

enter image description here

Thanks in advance.

Upvotes: 1

Views: 116

Answers (1)

Akina
Akina

Reputation: 42854

Test

$content = Content::query();
if ($request->input('total') == '1') {
$content = $content->select(DB::raw('con_type, operation, ecu, ecu_sub, count(*) as `count`')
                   ->groupBy('operation', 'con_type', 'ecu', 'ecu_sub')
                   ->orderBy('operation', 'asc')
                   ->orderBy('con_type', 'asc')
                   ->get();
}

Upvotes: 1

Related Questions