Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

nshahadat's avatar

Convert MySql query to Laravel

Here my MySQL query (work in phpMyAdmin) :

SELECT workcenter, (SUM(w1+w2 +w3 +w4)/ (COUNT(DISTINCT(teknisi))*40*4) )*100 AS total FROM `team` GROUP by workcenter ORDER BY total

then, i try in Laravel Sintax like this below (not work) :

$sql = Team::groupBy('workcenter')->select('workcenter', \DB::raw('(SUM(w1+w2+w3+w4)/ (COUNT(DISTINCT(teknisi))*30*4) )*100 AS total'))
            ->OrderBy('total', 'Desc')
            ->get();

When i run the laravel sintax, its doesn't show any errors, but the output is nothing..

1 like
2 replies
tisuchi's avatar
tisuchi
Best Answer
Level 70

Try this-

$sql = DB::table('team')
                     ->select(DB::raw('workcenter, (SUM(w1+w2 +w3 +w4)/ (COUNT(DISTINCT(teknisi))*40*4) )*100 as total'))
                     ->orderBy('total', 'desc')
                     ->groupBy('workcenter')
                     ->get();

4 likes

Please or to participate in this conversation.