繁体   English   中英

如何从 php laravel 中的两个表中获取多个值?

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM