how to convert this php native to laravel code ? this code use moving average method, is there anyone who can help solve my problem?

hello guys have a project and I'm having trouble creating my project. I have native PHP code and I can't change it to the Laravel programming language. My program uses the moving average method and for forecasting. Please help me how to fix it. I use the laravel framework.

<?php
$tahun = $_POST['tahun'];

**$rows = mysqli_query($db,"SELECT tahun,nm_brg,dt_actual FROM tb_actual tc 
                    INNER JOIN tb_barang brg ON tc.kd_brg = brg.kd_brg 
                    INNER JOIN tb_tahun thn ON thn.id_tahun = tc.id_tahun WHERE   thn.tahun <= '$tahun' AND brg.kd_brg = '$_GET[id]'");**
while ($rw = mysqli_fetch_object($rows)){
  $TAHUN[$rw->tahun]=$rw->dt_actual;
}
$chr = mysqli_query($db,"SELECT * FROM tb_tahun");
while ($chrs = mysqli_fetch_object($chr)){
  if(!empty($TAHUN[$chrs->tahun])){
      $DATA[$chrs->tahun]=$TAHUN[$chrs->tahun];
  }else{
      $DATA[$chrs->tahun]=0;
  }

}

$rwx = mysqli_query($db,"SELECT * FROM tb_tahun");
$start=1;
$dg = 0;
$sum=0;
while ($row = mysqli_fetch_object($rwx)):
  if($tahun>=$row->tahun):
  ?>

  <tr>
    <td><?=$start++?></td>
    <td><?=$row->tahun?></td>
    <td>
        <?php 
            if(!empty($DATA[$row->tahun]))
            {
                echo round ($DATA[$row->tahun]);
            }
            else
            {
                echo 0;
            }
        ?>
    </td>
    <td><?php 
      if(!empty($DATA[$row->tahun-2])){
        if ($DATA[$row->tahun-2]) {
          $awal = $DATA[$row->tahun-2];
        $akhir =  $DATA[$row->tahun-1];
        $hsl = ($awal + $akhir)/2;
        echo "(".$awal." + ".$akhir.") / 2 = ".$hsl;
          $_SESSION['angka'][$row->tahun]=$hsl;
        }else{
         echo ""; 
        }
      }else{echo "";}?>
    </td>
    <td><?php 
      if(!empty($DATA[$row->tahun-2])){
        if(!empty($DATA[$row->tahun]) && $DATA[$row->tahun]>0){
        echo $DATA[$row->tahun]-$hsl;

      }
      }else{echo "";}?>
    </td>
    <td><?php 
      if(!empty($DATA[$row->tahun-2])){
        $ses = $DATA[$row->tahun]-$hsl;
        if(!empty($DATA[$row->tahun])){
        if($ses<0){
          echo $ses*-1;
          $sum = $sum + $ses*-1;
        }else{
          echo $ses;
          $sum = $sum + $ses;
        }
        $dg++;
      }
      }else{echo "";}?>
    </td>

  </tr>
<?php endif;endwhile;?>

this is my php native code. i dont know how to convert this code to laravel. it will look like this

enter image description here

1 answer