2017-07-12 123 views
0

以下是我的表慈善組織,其類型,成員,成員名稱現在我想結果爲特定的慈善機構慈善的所有成員與其指定和慈善組織類型的慈善機構。加入多個表,使用Laravel關係的數據透視表

Charity 
--------------------- 
id  name  | 
--------------------- 
1  Xcharity | 
2  ycharity | 
--------------------- 

Charity_type 
--------------------- 
id  name  | 
--------------------- 
1  Masjid  | 
2  Mandir  | 
3  Madresha | 
--------------------- 

Designations 
--------------------- 
id  name  | 
--------------------- 
1  Trusty  | 
2  President | 
3  Member  | 
--------------------- 

Members 
----------------------------- 
id  name charity_id | 
----------------------------- 
1  nilesh  1  | 
2  Khyati  2  | 
----------------------------- 

Charitytype_designation_member 
----------------------------------------------------- 
charitytype_id  designation_id  member_id | 
----------------------------------------------------- 
    1     1     1  | 
    2     2     1  | 
    2     3     2  | 
----------------------------------------------------- 

我想陣列如下面詳細慈善機構及與構件構件是從其中慈善機構的類型和其指定爲特定慈善機構的類型。

Array 
(
    [id] => 1 
    [name] => Xcharity 
    [members] => Array 
     (
      [0] => Array 
       (
        [id] => 1 
        [name] => nilesh      
        [charity_id] => 2 
        [charity_type] => Masjid 
        [designation] => Trusty      
       ) 
     ) 
) 
+0

你可以請你展示你的模型關係。 –

+0

你的模型關係是什麼樣子的?當前的代碼不能幫助任何人回答你的問題。 –

回答

0

你可以嘗試這樣的事情

$contracts = Charity::with(['members' => function($member_model) { 
        $member_model->select('Members.id', 'Members.name', 'Members.charity_id', 'Charity_type.name as charity_type', 'Designations.name as designation') 
          ->join('Charitytype_designation_member', 'Charitytype_designation_member.member_id', '=', 'Members.id') 
          ->join('Designations', 'Charitytype_designation_member.designation_id', '=', 'Designations.id') 
          ->join('Charity_type', 'Charitytype_designation_member.charitytype_id ', '=', 'Charity_type.id'); 
       }])->get(); 

我想在你的慈善模式members關係。
我希望它能幫到你

相關問題