简体   繁体   English

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

[英]how to get multiple values from two table in php laravel in a table?

I have two Tables namly AllotmentApps and Groups.我有两个表,分别是 AllotmentApps 和 Groups。 I want to extract values from these two tables as shown in Final Table.我想从这两个表中提取值,如最终表所示。

I've achieved my goal about 70%.我已经实现了大约 70% 的目标。 The only problem that I'm facing to show the Group CGPA.我面临的唯一问题是向 Group CGPA 展示。 In Each Group, The Group CGPA must be show that.在每个组中,必须显示该组 CGPA。 belongs to the same Group ID.属于同一个组 ID。 For example if GrpID 1 have value 3.5 , then in the final table is must show only in the group where GrpId is 1. But in my case it is not according to the GrpID.例如,如果GrpID 1的值为3.5 ,那么在最终表中必须仅在 GrpId 为 1 的组中显示。但在我的情况下,它不是根据 GrpID。

Final Table决赛桌

Below is the code of my Controller:下面是我的 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'));
}

Here is the code for view.blade.php这是 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

You Join is not correct.你加入不正确。 Please read the doc .请阅读文档

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

You ask for joining AllotmentApps with Groups.您要求通过 Groups 加入 AllotmentApps。 Thats fine.没关系。 But then you said that both primary keys have to be the same.但是后来你说两个主键必须相同。 So you join for example Groups.id = 1 with AllotmentApps.id = 1所以你加入例如 Groups.id = 1 和 AllotmentApps.id = 1

I assume that you want, to compare foreign and primary key:我假设您想要比较外键和主键:

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

(Or vice versa. I do not know your database model) (反之亦然。我不知道你的数据库模型)

 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'));
}

it was AllotmentApps.grpID instead AllotmentApps.id它是AllotmentApps.grpID而不是AllotmentApps.id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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