使用Laravel关系连接多个表,数据透视表

时间:2017-07-12 12:04:16

标签: php laravel laravel-5.4 laravel-eloquent

下面是我的表慈善机构,它的类型,成员,成员名称现在我想要特定慈善机构的所有成员慈善机构的名称和数组中的慈善类型。

GraphQL

我想要数组如下慈善机构细节,其成员与成员是慈善机构类型及其对特定慈善机构类型的指定。

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

1 个答案:

答案 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关系。 我希望它可以帮到你