【问题标题】:how to join 4 tables in php laravel?如何在 php laravel 中加入 4 个表?
【发布时间】:2021-01-10 03:03:24
【问题描述】:

我想从 4 个表中提取数据。

如 sname、cgpa、Group_Cgpa、Room_No 和旅馆名称。

表关系如下。

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

群组有:id,Group_CGPA

房间有:id、HostelID(来自 Hostel)和 ResidentID(来自 Group id)。

Hostels 有:id、Name、Total_Rooms。

我在加入表格时遇到了问题。加入 AllotmentApps 和 Groups 工作正常,但是当我加入 Room and Hostel Table 时出现问题。

AllotmentApps 和群组的加入

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

【问题讨论】:

    标签: php database laravel join


    【解决方案1】:
    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'));
          }
     
    

    【讨论】:

      【解决方案2】:

      试试这个

      $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();
      

      【讨论】:

        猜你喜欢
        • 2013-10-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-23
        • 1970-01-01
        • 2019-10-06
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多