如何获得相同的ID的平均值并将其发布到Laravel的其他表格
我有两张雄辩的桌子,这是阴茎的桌子和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>
谢谢。
编辑:更新的代码和查看
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:
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?
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:
Thank you.
Edit: updated code and view
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论