Skip to content
Advertisement

Totaling the values of columns and get the average to save it on another table

I have a table work in a database that have columns score_1, score_2, score_3, and score_4. I want to get the total value and save it to database as a total_score. And from that total_score I want to get the average matching the work data with the vendor ID. How to achieve this? At the moment I have, the total_score as a raw view (not saved to the database). Here is the code for the table:

<section class="content">
    <div class="container-fluid">
        <div class="row">
            <div class="col-12">
              <div class="card">
                <div class="card-header">
                  <h3 class="card-title"><b>{{$penyedia->nama}}</b></h3> <br>
                  {{-- @foreach ($pekerjaan as $pekerjaans)
                  @endforeach
                  @php
                    $nilaiPenyedia = 0.0;
                    $totalNilai = 0.0;
                    $totalNilai = $pekerjaans->nilai_1 + $pekerjaans->nilai_2 + $pekerjaans->nilai_3 + $pekerjaans->nilai_4; 

                    $nilaiPenyedia += $totalNilai; 
                  @endphp --}} // Supposed to get the average but not working properly 
                  {{-- <h3 class="card-title">Nilai Total: <b>{{$nilaiPenyedia}}</b></h3> --}}
                </div>
                <!-- /.card-header -->
                <div class="card-body table-responsive">
                  <table id="tabelpekerjaan" class="table table-bordered">
                    <thead>
                      <tr>
                        <th style="width: 10px">No.</th>
                        <th>Paket Pekerjaan</th>
                        <th>Nama Perusahaan</th>
                        <th>Lokasi Pekerjaan</th>
                        <th>HPS</th>
                        <th>Nilai Kontrak</th>
                        <th>Nilai</th>
                        <th style="width: 120px">Aksi</th>
                      </tr>
                    </thead>
                    <tbody>
                      @php $no = 1; $totalNilai = 0.0; @endphp
                      @foreach ($pekerjaan as $pekerjaans)
                      <tr>
                        <td>{{$no++}}</td>
                        <td>{{$pekerjaans->pekerjaan}}</td>
                        <td>{{$pekerjaans->penyedia->nama}}</td>
                        <td>{{$pekerjaans->lokasi}}</td>
                        <td>Rp. {{number_format($pekerjaans->hps,0,',',',')}}</td>
                        <td>Rp. {{number_format($pekerjaans->nilai_kontrak,0,',',',')}}</td>
                        @php
                        $totalNilai = $pekerjaans->nilai_1 + $pekerjaans->nilai_2 + $pekerjaans->nilai_3 + $pekerjaans->nilai_4;
                        @endphp //Get the total_score but not saved to the database.
                        <td>{{$totalNilai}}</td>
                        <td>
                            <a href="/nilai/{{$pekerjaans->id}}" type="button" class="btn btn-outline-primary">Beri Penilaian</a>
                        </td>
                      </tr>
                      @endforeach
                    </tbody>
                  </table>
                </div>
                <!-- /.card-body -->
              </div>
              <!-- /.card -->
              <div class="card-footer">
                <a href="/datanilai_penyedia" type="button" class="btn btn-outline-secondary">Kembali</a>
              </div>
            
    </div>
</section>

Here is the form where the user input the data

<section class="content">
    <div class="container-fluid">
      <div class="row">
        <!-- left column -->
        <div class="col-12">
          <!-- general form elements -->
          <div class="card card-primary">
            <!-- /.card-header -->
            <!-- form start -->
                <div class="card-body">
                  <div class="form-group">
                    <label for="exampleInputEmail1">Paket Pekerjaan</label>
                    <input type="text" name="pekerjaan" class="form-control" id="exampleInputEmail1" placeholder="Pekerjaan" value="{{$pekerjaan->pekerjaan}}" disabled>
                  </div>
                  <div class="form-group">
                    <label for="exampleInputPassword1">Nama Perusahaan</label>
                    <input name="penyedia_id" class="form-control" style="width: 100%;" value="{{$pekerjaan->penyedia->nama}}" disabled>
                  </div>
                  <div class="form-group">
                      <label>Lokasi</label>
                      <textarea name="lokasi" class="form-control" rows="3" placeholder="Alamat" disabled>{{$pekerjaan->lokasi}}</textarea>
                  </div>
                  <div class="row">
                      <div class="col-sm-6">
                        <div class="form-group">
                          <label for="exampleInputEmail1">Nilai Kontrak</label>
                          <input type="number" step=".01" name="nilai_kontrak" class="form-control" id="exampleInputEmail1" placeholder="Pekerjaan" value="{{$pekerjaan->nilai_kontrak}}" disabled>
                        </div>
                      </div>
                      <div class="col-sm-6">
                        <div class="form-group">
                          <label for="exampleInputEmail1">HPS</label>
                          <input type="number" step=".01" name="hps" class="form-control" id="exampleInputEmail1" placeholder="Pekerjaan" value="{{$pekerjaan->hps}}" disabled>
                        </div>
                      </div>
                    </div>
                </div>
              <!-- /.card-body -->
            
          </div>
        </div>
    </section>

    <section class="content">
      <div class="container-fluid">
        <form action="/updatenilaipekerjaan/{{$pekerjaan->id}}" method="POST" enctype="multipart/form-data">
          @csrf
        <div class="card card-default collapsed-card">
          <div class="card-header">
            <h3 class="card-title"><b>1. Kualitas dan Kuantitas Pekerjaan (30%)</b></h3>

            <div class="card-tools">
              <button type="button" class="btn btn-tool" data-card-widget="collapse">
                <i class="fas fa-plus"></i>
              </button>
            </div>
          </div>
          <!-- /.card-header -->
          <div class="card-body">
                <div class="form-group">
                  <div class="custom-control custom-radio">
                    <input class="custom-control-input" type="radio" id="customRadio1" name="nilai_1" value="0.3">
                    <label for="customRadio1" class="custom-control-label">>50% hasil pekerjaan memerlukan perbaikan/penggantian agar sesuai dengan ketentuan dalam kontrak. (Skor 1)</label>
                  </div>
                  <br>
                  <div class="custom-control custom-radio">
                    <input class="custom-control-input" type="radio" id="customRadio2" name="nilai_1" value="0.6">
                    <label for="customRadio2" class="custom-control-label">≤50% hasil pekerjaan memerlukan perbaikan/penggantian agar sesuai dengan ketentuan dalam kontrak. (Skor 2)</label>
                  </div>
                  <br>
                  <div class="custom-control custom-radio">
                    <input class="custom-control-input" type="radio" id="customRadio3" name="nilai_1" value="0.9">
                    <label for="customRadio3" class="custom-control-label">100% hasil pekerjaan sesuai dengan ketentuan dalam kontrak. (skor 3)</label>
                  </div>
                </div>
            <!-- /.row -->
          </div>
          <!-- /.card-body -->
        </div>
        <!-- /.card -->

        <!-- SELECT2 EXAMPLE -->
        <div class="card card-default collapsed-card">
          <div class="card-header">
            <h3 class="card-title"><b>2. Layanan dengan indikator komunikasi dan tingkat respon (20%)</b></h3>

            <div class="card-tools">
              <button type="button" class="btn btn-tool" data-card-widget="collapse">
                <i class="fas fa-plus"></i>
              </button>
            </div>
          </div>
          <!-- /.card-header -->
          <div class="card-body">
            <div class="form-group">
              <div class="custom-control custom-radio">
                <input class="custom-control-input" type="radio" id="customRadio4" name="nilai_2" value="0.2">
                <label for="customRadio4" class="custom-control-label">a. Penyedia Lambat memberti tanggapan positif atas permintaan PPK; dan <br>
                  b. Penyedia sulit diajak berdiskusi dalam penyelesaian pelaksanaan pekerjaan. (Skor 1)</label>
              </div>
              <br>
              <div class="custom-control custom-radio">
                <input class="custom-control-input" type="radio" id="customRadio5" name="nilai_2" value="0.4">
                <label for="customRadio5" class="custom-control-label">a. Merespon permintaan dengan penyelesaian sesuai dengan yang diminta; atau <br>
                  b. Penyedia mudah dihubungi dan berdiskusi dalam penyelesaian pelaksanaan pekerjaan. (Skor 2)</label>
              </div>
              <br>
              <div class="custom-control custom-radio">
                <input class="custom-control-input" type="radio" id="customRadio6" name="nilai_2" value="0.6">
                <label for="customRadio6" class="custom-control-label">a. Merespon permintaan dengan penyelesaian sesuai dengan yang diminta; dan <br>
                  b. Penyedia mudah dihubungi dan berdiskusi dalam penyelesaian pelaksanaan pekerjaan. (Skor 3)</label>
              </div>
            </div>
          </div>
          <!-- /.card-body -->
        </div>
        <!-- /.card -->

        <div class="card card-default collapsed-card">
          <div class="card-header">
            <h3 class="card-title"><b>3. Waktu dengan indikator ketepatan (30%)</b></h3>

            <div class="card-tools">
              <button type="button" class="btn btn-tool" data-card-widget="collapse">
                <i class="fas fa-plus"></i>
              </button>
            </div>
          </div>
          <!-- /.card-header -->
          <div class="card-body">
            <div class="form-group">
              <div class="custom-control custom-radio">
                <input class="custom-control-input" type="radio" id="customRadio7" name="nilai_3" value="0.3">
                <label for="customRadio7" class="custom-control-label">Penyelesaian pekerjaan terlambat melebihi 50 (lima puluh) hari kalender dari waktu yang telah ditetapkan dalam kontrak karena kesalahan penyedia. (Skor 1)</label>
              </div>
              <br>
              <div class="custom-control custom-radio">
                <input class="custom-control-input" type="radio" id="customRadio8" name="nilai_3" value="0.6">
                <label for="customRadio8" class="custom-control-label">Penyelesaian pekerjaan terlambat sampai 50 hari dari kontrak kesalahan Penyedia. (Skor 2)</label>
              </div>
              <br>
              <div class="custom-control custom-radio">
                <input class="custom-control-input" type="radio" id="customRadio9" name="nilai_3" value="0.9">
                <label for="customRadio9" class="custom-control-label">Penyelesaian pekerjaan sesuai dengan waktu dalam kontrak atau lebih cepat sesuai kebutuhan PPK. (Skor 3)</label>
              </div>
            </div>
          </div>
          <!-- /.card-body -->
        </div>
        <!-- /.card -->

        <div class="card card-default collapsed-card">
          <div class="card-header">
            <h3 class="card-title"><b>4. Biaya dengan indikator kemampuan pengendalian biaya (20%)</b></h3>

            <div class="card-tools">
              <button type="button" class="btn btn-tool" data-card-widget="collapse">
                <i class="fas fa-plus"></i>
              </button>
            </div>
          </div>
          <!-- /.card-header -->
          <div class="card-body">
            <div class="form-group">
              <div class="custom-control custom-radio">
                <input class="custom-control-input" type="radio" id="customRadio10" name="nilai_4" value="0.2">
                <label for="customRadio10" class="custom-control-label">a. Tidak menginformasikan sejak awal kondisi/kejadian yang berpotensi menambah biaya; dan <br>
                  b. Mengajukan perubahan kontrak yang berdampak penambahan total biaya tanpa alasan memadai sehingga di tolak PPK. (Skor 1)</label>
              </div>
              <br>
              <div class="custom-control custom-radio">
                <input class="custom-control-input" type="radio" id="customRadio11" name="nilai_4" value="0.4">
                <label for="customRadio11" class="custom-control-label">a. Melakukan salah satu kondisi pada kriteria cukup. (Skor 2)</label>
              </div>
              <br>
              <div class="custom-control custom-radio">
                <input class="custom-control-input" type="radio" id="customRadio12" name="nilai_4" value="0.6">
                <label for="customRadio12" class="custom-control-label">Telah melakukan pengendalian biaya dengan baik dengan menginformasikan sejak awal atas kondisi yang berpotensi menambah biaya dan perubahan kontrak sudah didasari dengan alasan yang dapat dipertanggungjawabkan. (Skor 3)</label>
              </div>
            </div>
          </div>
          <!-- /.card-body -->
        </div>
        <!-- /.card -->
        <div class="card-footer">
          <a href="/nilaipekerjaan/{{$pekerjaan->penyedia->id}}" type="button" class="btn btn-outline-secondary">Kembali</a>
          <button type="submit" class="btn btn-outline-primary delete">Submit</button>
        </div>
      </form>
      
      </div>
      </section>

model

work.php

<?php

namespace AppModels;

use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel as Eloquent;

class Pekerjaan extends Eloquent
{
    use HasFactory;

    protected $guarded = [];

    public function penyedia(){
        return $this->belongsTo(Penyedia::class, 'penyedia_id');
   }

}

vendor.php

<?php

namespace AppModels;

use IlluminateDatabaseEloquentFactoriesHasFactory;
use IlluminateDatabaseEloquentModel as Eloquent;

class Penyedia extends Eloquent
{
    use HasFactory;

    protected $guarded = [];

    public $timestamps = false;

    public function pekerjaans(){
       return $this->hasMany(Pekerjaan::class);
   }
}

AdminController

public function update_nilaipekerjaan(Request $request, $id){
        $pekerjaan = Pekerjaan::find($id);
        //$pekerjaan->update($request->all());

        $pekerjaan->nilai_1=$request->nilai_1;
        $pekerjaan->nilai_2=$request->nilai_2;
        $pekerjaan->nilai_3=$request->nilai_3;
        $pekerjaan->nilai_4=$request->nilai_4; //Get the value and save it to database

        $pekerjaan->save();
        
        return redirect()->back()->with('message','Nilai Pekerjaan Berhasil diupdate!');
    }

Database https://imgur.com/a/N6NlcwJ Work Table

Vendor Table

Here is what I want to achieve https://imgur.com/a/fk4Yq92 What I want to achieve

Advertisement

Answer

For total score add this code in update_nilaipekerjaan() function befor save function :

$pekerjaan->nilai_total=$request->nilai_1+$request->nilai_2+$request->nilai_3+$request->nilai_4;

and if you want to get the average add this codde :

$pekerjaan->nilai=($request->nilai_1+$request->nilai_2+$request->nilai_3+$request->nilai_4)/4;
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement