【问题标题】:Unexpected results in eloquent query雄辩的查询中的意外结果
【发布时间】:2015-02-14 16:36:44
【问题描述】:

我在我的 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"
                }
            }
        ]
    }
]

在上面的对象中你可以看到我得到了返回给我的组织项目和客户,但是客户有嵌套项目,即他们也相关的项目,但我想返回与他们是客户嵌套的项目相关也可以吗?

【问题讨论】:

  • 是的,您显示的代码将返回您所期望的。显示关系和执行,输出 json 结果。

标签: php mysql laravel relational-database eloquent


【解决方案1】:

关于命名的注释。如果您的项目只能有一个客户,则将关系称为“客户”而不是“客户”会更好。

项目模型

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

这种方式可读性更强,可以这样称呼

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

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

查看Eloquent 文档以获取有关关系的更多信息

【讨论】:

    【解决方案2】:
    $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
    

    【讨论】:

    • 闭包是多余的。
    • 嘿,你能解释一下它是如何冗余的吗?
    • with('projects.client') 就足够了。您建议的闭包用于在关系查询上添加约束,当您只想加载嵌套关系时不需要。
    猜你喜欢
    • 2014-09-16
    • 2021-01-12
    • 2017-04-18
    • 1970-01-01
    • 2015-07-07
    • 2018-01-26
    • 2014-04-12
    • 2021-07-23
    相关资源
    最近更新 更多