[英]how to get multiple values from two table in php laravel in a table?
我有兩個表,分別是 AllotmentApps 和 Groups。 我想從這兩個表中提取值,如最終表所示。
我已經實現了大約 70% 的目標。 我面臨的唯一問題是向 Group CGPA 展示。 在每個組中,必須顯示該組 CGPA。 屬於同一個組 ID。 例如,如果GrpID 1的值為3.5 ,那么在最終表中必須僅在 GrpId 為 1 的組中顯示。但在我的情況下,它不是根據 GrpID。
下面是我的 Controller 的代碼:
public function View_GroupStudent()
{
$allot_apps = AllotmentApps::join('Groups','Groups.id','AllotmentApps.id')->orderBy('grpID')->get()->groupBy(
function($item) {
return $item->grpID;
}
);
//dd([$allot_apps->name]);
return view('deny', compact('allot_apps'));
}
這是 view.blade.php 的代碼
<table class="table table-bordered">
@foreach ($allot_apps as $GID => $member_list)
<tr>
<th colspan="3"
style="background-color: #F7F7F7 ; text-align: center;">
Grp Id:{{ $GID }} , Members: {{ $member_list->count() }}
<tr style="background-color: #F7F7F7 ;" >
<th > Name</th>
<th> Student CGPA</th>
<th > Group CGPA</th>
</tr>
</th>
</tr>
@foreach ($member_list as $user)
<tr>
<td>{{ $user->sname }}</td>
<td>{{ $user->cgpa }}</td>
<td >{{ $user->name}}</td>
<!-- <td>{{ $user->grpID }}</td> -->
</tr>
@endforeach
@endforeach
你加入不正確。 請閱讀文檔。
AllotmentApps::join('Groups','Groups.id','AllotmentApps.id')
您要求通過 Groups 加入 AllotmentApps。 沒關系。 但是后來你說兩個主鍵必須相同。 所以你加入例如 Groups.id = 1 和 AllotmentApps.id = 1
我假設您想要比較外鍵和主鍵:
AllotmentApps::join('Groups','Groups.id','AllotmentApps.group_id')
(反之亦然。我不知道你的數據庫模型)
public function View_GroupStudent()
{
$allot_apps = AllotmentApps::join('Groups','Groups.id','AllotmentApps.grpID')->orderBy('grpID')->get()->groupBy(
function($item) {
return $item->grpID;
}
);
// dd([$allot_apps]);
return view('deny', compact('allot_apps'));
}
它是AllotmentApps.grpID
而不是AllotmentApps.id
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.