如何获得相同的ID的平均值并将其发布到Laravel的其他表格

发布于 2025-02-13 20:53:50 字数 3117 浏览 1 评论 0原文

我有两张雄辩的桌子,这是阴茎的桌子和Pekerjaan的桌子。 Penyedia Hasmany Pekerjaans和Pekerjaan属于Penyedia 。在表格中有外键是“ Penyedia_id”和“ Nilai_total”(总分)。这是我的佩克贾安桌子的图片:

我想以相同的'Penyedia_id'获得总分的平均值(就像下图)并通过/张贴并将其显示到阴茎的桌子上。在Laravel中,我该如何做到这一点?

这是我在AdminController中的代码:

public function tabelnilai_penyedia(){
        $penyedia = Penyedia::all();
        $pekerjaan = Pekerjaan::all();

        //$totalAvg=$penyedia->pekerjaans()->avg('nilai_total');
        $totalAvg = Pekerjaan::selectRaw("penyedia_id, AVG(nilai_total) AS avg")->groupBy('penyedia_id')->get();

        return view('admin.datanilai_penyedia', compact('penyedia', 'pekerjaan','totalAvg'));
    }

这是刀片文件对于表视图:

<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">Tabel Penilaian Penyedia</h3>
                  
                </div>
                <!-- /.card-header -->
                <div class="card-body table-responsive">
                  <table id="tabelpenyedia" class="table table-bordered">
                    <thead>
                      <tr>
                        <th style="width: 10px">No.</th>
                        <th>Nama Penyedia</th>
                        <th>Nilai</th>
                        <th style="width: 120px">Aksi</th>
                      </tr>
                    </thead>
                    <tbody>
                      @php $no = 1; @endphp
                      @foreach ($penyedia as $penyedias)
                      <tr>
                        <td>{{$no++}}</td>
                        <td>{{$penyedias->nama}}</td>
                        <td>{{$totalAvg}}</td>
                        <td>
                            <a href="/nilaipekerjaan/{{$penyedias->id}}" type="button" class="btn btn-primary btn-block btn-outline-primary">Detail</a>
                        </td>
                      </tr>
                      @endforeach
                    </tbody>
                  </table>
                </div>
                <!-- /.card-body -->
              </div>
              <!-- /.card -->
  
            
    </div>
</section>

这里表看起来像ATM:

谢谢。

编辑:更新的代码和查看

I have two eloquent table, which is the Penyedia's table and Pekerjaan's table. Penyedia hasMany Pekerjaans and Pekerjaan belongsTo Penyedia. Inside the table there are foreign key which is the 'penyedia_id' and the 'nilai_total' (total score). Here is the picture of my Pekerjaan's Table:
Pekerjaan's table

I want to get the average of the total score with the same 'penyedia_id' (like the picture below) and pass/post and show it to the Penyedia's Table. How can I do that in actual view in Laravel?
Avg with the same penyedia_id

Here is my code in AdminController:

public function tabelnilai_penyedia(){
        $penyedia = Penyedia::all();
        $pekerjaan = Pekerjaan::all();

        //$totalAvg=$penyedia->pekerjaans()->avg('nilai_total');
        $totalAvg = Pekerjaan::selectRaw("penyedia_id, AVG(nilai_total) AS avg")->groupBy('penyedia_id')->get();

        return view('admin.datanilai_penyedia', compact('penyedia', 'pekerjaan','totalAvg'));
    }

Here is the blade file for the table view:

<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">Tabel Penilaian Penyedia</h3>
                  
                </div>
                <!-- /.card-header -->
                <div class="card-body table-responsive">
                  <table id="tabelpenyedia" class="table table-bordered">
                    <thead>
                      <tr>
                        <th style="width: 10px">No.</th>
                        <th>Nama Penyedia</th>
                        <th>Nilai</th>
                        <th style="width: 120px">Aksi</th>
                      </tr>
                    </thead>
                    <tbody>
                      @php $no = 1; @endphp
                      @foreach ($penyedia as $penyedias)
                      <tr>
                        <td>{{$no++}}</td>
                        <td>{{$penyedias->nama}}</td>
                        <td>{{$totalAvg}}</td>
                        <td>
                            <a href="/nilaipekerjaan/{{$penyedias->id}}" type="button" class="btn btn-primary btn-block btn-outline-primary">Detail</a>
                        </td>
                      </tr>
                      @endforeach
                    </tbody>
                  </table>
                </div>
                <!-- /.card-body -->
              </div>
              <!-- /.card -->
  
            
    </div>
</section>

Here what the table is looks like atm:
enter image description here

Thank you.

Edit: updated code and view

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文