简体   繁体   中英

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

I have two Tables namly AllotmentApps and Groups. I want to extract values from these two tables as shown in Final Table.

I've achieved my goal about 70%. The only problem that I'm facing to show the Group CGPA. In Each Group, The Group CGPA must be show that. belongs to the same Group 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.

Final Table

Below is the code of my 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

<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. 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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