gpt4 book ai didi

php - Eloquent 查询中的意外结果

转载 作者:行者123 更新时间:2023-11-30 22:53:05 25 4
gpt4 key购买 nike

我在我的 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有关关系的更多信息的文档

关于php - Eloquent 查询中的意外结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27503411/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com