【问题标题】:how to get multiple values from two table in php laravel in a table?如何从一个表中的php laravel中的两个表中获取多个值?
【发布时间】:2020-11-14 10:28:28
【问题描述】:

我有两个表,分别是 AllotmentApps 和 Groups。 我想从这两个表中提取值,如最终表所示。

我已经实现了大约 70% 的目标。我面临的唯一问题是向 Group CGPA 展示。在每个组中,必须显示该组 CGPA。属于同一个组 ID。例如,如果 GrpID 1 的值为 3.5 ,则在最终表格中必须仅在 GrpId 为 1 的组中显示。 但在我的情况下,它不是根据 GrpID。

Final Table

以下是我的控制器的代码:

  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

【问题讨论】:

    标签: php laravel laravel-blade


    【解决方案1】:

    您的加入不正确。请阅读doc

    AllotmentApps::join('Groups','Groups.id','AllotmentApps.id')
    

    您要求通过 Groups 加入 AllotmentApps。没关系。但是后来你说两个主键必须相同。所以你加入例如 Groups.id = 1 和 AllotmentApps.id = 1

    我假设您想要比较外键和主键:

    AllotmentApps::join('Groups','Groups.id','AllotmentApps.group_id')
    

    (反之亦然。我不知道你的数据库模型)

    【讨论】:

      【解决方案2】:
       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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-11-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-28
        • 1970-01-01
        • 1970-01-01
        • 2016-11-02
        相关资源
        最近更新 更多