我在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"
}
}
]
}
]
在上面的对象中,您可以看到我让组织项目和客户端返回给我,但客户端有嵌套项目,即它们相关的项目,但我想返回与客户端嵌套的项目这也有可能吗?
答案 0 :(得分:1)
关于命名的一个注释。如果您的项目只能有一个客户,那么如果称为“客户”而不是“客户”,这种关系会更好。
项目模型
//Client relationship in Project model
client()
{
$this->belongsTo('Client')
}
这种方式更具可读性,你可以这样称呼它
Organisation::with('projects.client', 'clients', 'users')->get()
您需要组织模型中的关系项目,客户和用户。
查看Eloquent文档以获取有关关系的更多信息
答案 1 :(得分:-1)
$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