簡體   English   中英

如何在 php laravel 中加入 4 個表?

[英]how to join 4 tables in php laravel?

我想從 4 個表中提取數據。

像 sname、cgpa、Group_Cgpa、Room_No 和旅館名稱。

表的關系如下。

AllotmentApps有:id、sname、cgpa 和 grpID(來自 Group id 表)列。

有:id,Group_CGPA

房間有:id、HostelID(來自旅館)和ResidentialID(來自組ID)。

旅館有:id、Name、Total_Rooms。

我在加入表格時遇到問題。 Join with AllotmentApps and Groups 工作正常,但當我加入 Room 和 Hostel Table 時出現問題。

加入分配應用程序和組

public function FinalList()
{   
   $allot_apps = AllotmentApps::join('Groups','Groups.id','AllotmentApps.grpID')->orderBy('Groups.Group_CGPA', 'Desc')->get()->groupBy(
      function($item) { 
        return $item->grpID;
    }
  ) ->take(3);

4 個表的連接

public function FinalList()
{   
   $allot_apps = AllotmentApps::select('sname','cgpa','Group_CGPA','Hostel_Name')->join('Groups','Groups.id','AllotmentApps.grpID')->join('Hostels','Hostels.id','Hostels.HostelID')->join('Rooms','Rooms.id','Hostel.HostelID')->orderBy('Groups.id', 'Asc')->get()->groupBy(
      function($item) { 
        return $item->grpID;
    }
  ) ->take(3);

嘗試這個

$allot_apps=AllotmentApps::select('AllotmentApps .sname','AllotmentApps .cgpa','Groups.Group_CGPA','tablename.Hostel_Name')
->join('Groups','Groups.id','=','AllotmentApps.grpID')
->join('Rooms','Rooms.ResidentID','=','Groups.id')
->join('Hostels','Hostels.id','=','Rooms.HostelID')
->orderBy('Groups.id', 'Asc')
->groupBy()
->get();
public function FinalList()
{  $capasity = Rooms::selectRaw('count(id) as c')->count();
   $allot_apps = Rooms::select('Hostel_Name','Rooms.id','Groups.Group_CGPA','AllotmentApps.sname','AllotmentApps.cgpa')->join('Hostels','Hostels.id','Rooms.HostelID')->join('Groups','Groups.id','Rooms.ResidentID')->join('AllotmentApps','Groups.id','AllotmentApps.grpID')->orderBy('Groups.Group_CGPA', 'Desc')->get()->groupBy(
      function($item) { 
        return $item->grpID;
    }
  ) ->take($capasity);
   // return $allot_apps;
   return view('x', compact('allot_apps'));
      }
 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM