雄辩查询中的意外结果


Unexpected results in eloquent query

我正在我的 laravel 代码中运行以下查询,

$organisations = Organisation::with('projects', 'projects.clients')
                     ->with('clients')
                     ->get();
    $organisations->load('users');

这正在我的组织表中加载,并且应该在任何相关模型中加载,因此我希望返回看起来像这样的对象,

organisation {
     projects {
         clients {
         }
     }
     users {
     }
}
基本上,一个组织可以有很多项目,一个

项目可以有一个客户,一个组织也可以有很多用户和许多客户。

想要从我的查询中返回的是每个项目返回客户端数据,但我没有得到,目前我正在返回以下对象。

[
    {
        "id": "114",
        "name": "XXXXXXX",
        "slug": "XXXXXXX",
        "information": "",
        "type": "organisation",
        "notifications": "0",
        "add_all": "0",
        "created_at": "2014-12-16 10:16:03",
        "updated_at": "2014-12-16 10:16:03",
        "clients": [
            {
                "id": "39",
                "name": "Simon's Test",
                "information": "",
                "address": null,
                "website": null,
                "email": null,
                "phone": null,
                "type": "client",
                "add_all": "0",
                "created_at": "2014-12-16 10:16:20",
                "updated_at": "2014-12-16 10:16:20",
                "user_id": "1",
                "owner_id": "114",
                "projects": [
                    {
                        "id": "56",
                        "name": "iOS Application",
                        "description": "An iOS application that will be available on iPhone and iPad. Something that will change the way we manage projects.",
                        "total_cost": "5000.00",
                        "start_date": "2014-01-01",
                        "finish_date": "2014-12-10",
                        "status": "1",
                        "sales_person": null,
                        "project_manager": null,
                        "client_id": "39",
                        "organisation_id": "114",
                        "owner_id": "114",
                        "user_id": "1",
                        "created_at": null,
                        "updated_at": "2014-12-15 13:20:07"
                    },
                    {
                        "id": "57",
                        "name": "Android Application",
                        "description": "An android application that will run on any android phone, upto a certain version.",
                        "total_cost": "6500.00",
                        "start_date": "2015-01-31",
                        "finish_date": "2015-03-19",
                        "status": "2",
                        "sales_person": null,
                        "project_manager": null,
                        "client_id": "39",
                        "organisation_id": "114",
                        "owner_id": "114",
                        "user_id": "1",
                        "created_at": null,
                        "updated_at": "2014-12-11 15:01:15"
                    },
                    {
                        "id": "58",
                        "name": "Java Application",
                        "description": "A windows phone application, to be released for the small market share that market has.",
                        "total_cost": "7500.00",
                        "start_date": "2014-12-12",
                        "finish_date": "2014-12-31",
                        "status": "3",
                        "sales_person": null,
                        "project_manager": null,
                        "client_id": "39",
                        "organisation_id": "114",
                        "owner_id": "114",
                        "user_id": "1",
                        "created_at": null,
                        "updated_at": "2014-12-11 15:01:15"
                    },
                    {
                        "id": "59",
                        "name": "OSX Application Beta",
                        "description": "A desktop application for the Mac. More feature rich than the native iOS apps, more like the web application for desktop.",
                        "total_cost": "20000.00",
                        "start_date": "2014-11-20",
                        "finish_date": "2014-12-19",
                        "status": "2",
                        "sales_person": null,
                        "project_manager": null,
                        "client_id": "39",
                        "organisation_id": "114",
                        "owner_id": "114",
                        "user_id": "1",
                        "created_at": null,
                        "updated_at": "2014-11-28 16:01:41"
                    }
                ],
                "users": []
            }
        ],
        "projects": [
            {
                "id": "56",
                "name": "iOS Application",
                "description": "An iOS application that will be available on iPhone and iPad. Something that will change the way we manage projects.",
                "total_cost": "5000.00",
                "start_date": "2014-01-01",
                "finish_date": "2014-12-10",
                "status": "1",
                "sales_person": null,
                "project_manager": null,
                "client_id": "39",
                "organisation_id": "114",
                "owner_id": "114",
                "user_id": "1",
                "created_at": null,
                "updated_at": "2014-12-15 13:20:07",
                "pivot": {
                    "organisation_id": "114",
                    "project_id": "56"
                }
            },
            {
                "id": "57",
                "name": "Android Application",
                "description": "An android application that will run on any android phone, upto a certain version.",
                "total_cost": "6500.00",
                "start_date": "2015-01-31",
                "finish_date": "2015-03-19",
                "status": "2",
                "sales_person": null,
                "project_manager": null,
                "client_id": "39",
                "organisation_id": "114",
                "owner_id": "114",
                "user_id": "1",
                "created_at": null,
                "updated_at": "2014-12-11 15:01:15",
                "pivot": {
                    "organisation_id": "114",
                    "project_id": "57"
                }
            },
            {
                "id": "58",
                "name": "Java Application",
                "description": "A windows phone application, to be released for the small market share that market has.",
                "total_cost": "7500.00",
                "start_date": "2014-12-12",
                "finish_date": "2014-12-31",
                "status": "3",
                "sales_person": null,
                "project_manager": null,
                "client_id": "39",
                "organisation_id": "114",
                "owner_id": "114",
                "user_id": "1",
                "created_at": null,
                "updated_at": "2014-12-11 15:01:15",
                "pivot": {
                    "organisation_id": "114",
                    "project_id": "58"
                }
            },
            {
                "id": "59",
                "name": "OSX Application Beta",
                "description": "A desktop application for the Mac. More feature rich than the native iOS apps, more like the web application for desktop.",
                "total_cost": "20000.00",
                "start_date": "2014-11-20",
                "finish_date": "2014-12-19",
                "status": "2",
                "sales_person": null,
                "project_manager": null,
                "client_id": "39",
                "organisation_id": "114",
                "owner_id": "114",
                "user_id": "1",
                "created_at": null,
                "updated_at": "2014-11-28 16:01:41",
                "pivot": {
                    "organisation_id": "114",
                    "project_id": "59"
                }
            }
        ],
        "users": [
            {
                "id": "1",
                "email": "simon@pikcells.com",
                "first_name": "Simon",
                "last_name": "Ainley",
                "display_name": "simonainley",
                "initials": "SA",
                "remember_me": null,
                "active": "1",
                "invite_code": null,
                "forgotten_code": null,
                "login_type": "normal",
                "api_token": null,
                "created_at": "-0001-11-30 00:00:00",
                "updated_at": "2014-12-12 11:53:53",
                "deleted_at": null,
                "pivot": {
                    "organisation_id": "114",
                    "user_id": "1",
                    "is_admin": "1"
                }
            }
        ]
    }
]

在上面的对象中,您可以看到我将组织项目和客户返回给我,但是客户有嵌套项目,即它们也与哪些项目相关,但我想返回与它们相关的客户端嵌套的项目,这可能吗?

关于命名的一个说明。如果您的项目只能有一个客户,那么如果称为"客户"而不是"客户",则关系会更好。

项目模型

//Client relationship in Project model
 client()
 {
    $this->belongsTo('Client')
 }

这种方式更具可读性,您可以这样称呼它

Organisation::with('projects.client', 'clients', 'users')->get()

您需要组织模型中的项目、客户和用户的关系。

查看 Eloquent 文档,了解有关关系的更多信息

$organisations = Organisation::with(array('Projects'=>function($q){
                      $q->with('Client')
                 },'Clients','Users'))->get()                                          
You can fetch expected output using above code
You have to define 3 relationships in Organization model : Projects,Clients,Users
You have to define 1 relationship in Project model : Client