skip to Main Content

Here my MySQL query (work in phpMyAdmin) :

    SELECT waktu_kerusakan, workcenter, COUNT(workcenter) AS jumlah_repair 
    FROM repair WHERE year(waktu_kerusakan)='2019'  GROUP BY workcenter , 
    month(waktu_kerusakan) BETWEEN 1 and 6 Order By jumlah_repair Desc

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

    $sql = Main::groupBy('workcenter')->select('workcenter', DB::raw('count(*) as frekuensi'))
                ->whereYear('waktu_kerusakan', 'like',  "%".$tahun."%")
                ->OrderBy('frekuensi', 'Desc')
                ->groupBy(DB::raw("MONTH(waktu_kerusakan)"), [1, 6])
                ->get();

Please anyone help me to convert the MySQL query to Laravel Syntax. Thank you!

2

Answers


  1. Try this.

        $repair = DB::table('repair')
        ->select('waktu_kerusakan','workcenter', DB::raw('COUNT(workcenter) AS jumlah_repair'))
    ->whereYear('waktu_kerusakan', $tahun)
        ->groupBy(DB::raw("MONTH(waktu_kerusakan)"), 'workcenter')
      ->whereIn(DB::raw('MONTH("waktu_kerusakan")'),[1, 6])
        ->orderBy('frekuensi', 'DESC')
        ->get()
    
    Login or Signup to reply.
  2. Try this query :

    $sql = Main::select('workcenter', DB::raw('count(*) as frekuensi'),DB::raw('MONTH(waktu_kerusakan) as waktu_kerusakan_month'))
                ->where('waktu_kerusakan',$tahun)
                ->whereBetween('waktu_kerusakan_month',[1, 6])
                ->groupBy('workcenter','waktu_kerusakan_month')
                ->OrderBy('jumlah_repair', 'Desc')
                ->get();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search