[英]Laravel Eloquent ->load() not returning expected results
I have relationship that is setup like this, 我有这样设置的关系,
Organisation.php Organisation.php
public function projects()
{
return $this->belongsToMany('Project');
}
Project.php Project.php
public function organisations()
{
return $this->belongsToMany('Organisation');
}
I am running the following query, 我正在运行以下查询,
$project = Project::where('user_id', '=', ResourceServer::getOwnerId())
->whereNull('organisation_id')
->get(array(
'projects.id',
'projects.name',
'projects.description',
'projects.total_cost',
'projects.sales_person',
'projects.project_manager',
'projects.client_id',
'projects.start_date',
'projects.finish_date',
'projects.organisation_id',
'projects.created_at',
'projects.user_id',
'projects.owner_id',
'projects.status'
));
$project->load('salesperson');
$project->load('projectmanager');
$project->load('clients')->load('clients.users');
$project->load('organisations')->load('organisations.users');
$project->load('collaborators');
$project->load('user');
$owned_by_me = $project->toArray();
The results I get back looks like this, 我回来的结果看起来像这样,
[
{
"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",
"sales_person": "88",
"project_manager": "86",
"client_id": "39",
"start_date": "2014-01-01",
"finish_date": "2014-12-10",
"organisation_id": "114",
"created_at": "2014-12-15 13:20:07",
"status": "1",
"owner_id": "114",
"user_id": "1",
"is_admin": "1",
"salesperson": {
"id": "88",
"email": "stephen@pikcells.com",
"first_name": "Stephen",
"last_name": "Mooney",
"display_name": "stephenmooney",
"initials": "SM",
"remember_me": null,
"active": "1",
"invite_code": null,
"default_organisation": null,
"forgotten_code": null,
"login_type": "normal",
"api_token": null,
"created_at": "-0001-11-30 00:00:00",
"updated_at": "-0001-11-30 00:00:00",
"deleted_at": null
},
"projectmanager": {
"id": "86",
"email": "matt@pikcells.com",
"first_name": "Matt",
"last_name": "Fell",
"display_name": "Matt Fell",
"initials": "MF",
"remember_me": null,
"active": "1",
"invite_code": null,
"default_organisation": null,
"forgotten_code": null,
"login_type": "normal",
"api_token": null,
"created_at": "-0001-11-30 00:00:00",
"updated_at": "-0001-11-30 00:00:00",
"deleted_at": null
},
"clients": {
"id": "39",
"name": "Simon's Test",
"information": "",
"address": "This is an address and it can used for loads\r\n\r\nmultiple lines and stuff",
"website": "http://website.com",
"email": "email@email.com",
"phone": "07799009988",
"type": "client",
"add_all": "0",
"created_at": "2014-12-16 10:16:20",
"updated_at": "2014-12-17 16:22:55",
"user_id": "1",
"owner_id": "114",
"users": [
{
"id": "85",
"email": "richard@pikcells.com",
"first_name": "Richard",
"last_name": "Benson",
"display_name": "richardbenson",
"initials": "RB",
"remember_me": null,
"active": "1",
"invite_code": null,
"default_organisation": null,
"forgotten_code": null,
"login_type": "normal",
"api_token": null,
"created_at": "-0001-11-30 00:00:00",
"updated_at": "-0001-11-30 00:00:00",
"deleted_at": null,
"pivot": {
"client_id": "39",
"user_id": "85"
}
},
{
"id": "88",
"email": "stephen@pikcells.com",
"first_name": "Stephen",
"last_name": "Mooney",
"display_name": "stephenmooney",
"initials": "SM",
"remember_me": null,
"active": "1",
"invite_code": null,
"default_organisation": null,
"forgotten_code": null,
"login_type": "normal",
"api_token": null,
"created_at": "-0001-11-30 00:00:00",
"updated_at": "-0001-11-30 00:00:00",
"deleted_at": null,
"pivot": {
"client_id": "39",
"user_id": "88"
}
}
]
},
"user": {
"id": "1",
"email": "simon@pikcells.com",
"first_name": "Simons",
"last_name": "Ainley",
"display_name": "simonainley",
"initials": "SA",
"remember_me": null,
"active": "1",
"invite_code": null,
"default_organisation": "114",
"forgotten_code": "7bffdaa1b5b238f5ba8b13e46c53dc46",
"login_type": "normal",
"api_token": null,
"created_at": "-0001-11-30 00:00:00",
"updated_at": "2015-01-28 10:48:41",
"deleted_at": null
},
"organisations": [],
"collaborators": [
{
"id": "1",
"email": "simon@pikcells.com",
"first_name": "Simons",
"last_name": "Ainley",
"display_name": "simonainley",
"initials": "SA",
"remember_me": null,
"active": "1",
"invite_code": null,
"default_organisation": "114",
"forgotten_code": "7bffdaa1b5b238f5ba8b13e46c53dc46",
"login_type": "normal",
"api_token": null,
"created_at": "-0001-11-30 00:00:00",
"updated_at": "2015-01-28 10:48:41",
"deleted_at": null,
"pivot": {
"project_id": "56",
"user_id": "1"
}
},
{
"id": "91",
"email": "alan.test@testdomain.com",
"first_name": "Alan",
"last_name": "Test",
"display_name": "alantest",
"initials": "AT",
"remember_me": null,
"active": "1",
"invite_code": null,
"default_organisation": null,
"forgotten_code": null,
"login_type": "normal",
"api_token": null,
"created_at": "2014-12-12 11:13:07",
"updated_at": "2014-12-12 11:13:07",
"deleted_at": null,
"pivot": {
"project_id": "56",
"user_id": "91"
}
}
]
}
] ]
As you can see the organisations attribute is empty, but by the virtue that is has an organisation_id it should return an organisation. 如您所见,organizations属性为空,但是由于拥有organization_id,它应该返回一个organization。
Why would it not be doing? 为什么不这样做?
It's belongsTo
: 它
belongsTo
:
public function organisation()
{
return $this->belongsTo('Organisation');
}
belongsToMany
is a mm relation with pivot table, which is not the case here, since you have organisation_id
, right? belongsToMany
是与数据透视表的mm关系,此处不是这种情况,因为您具有organisation_id
,对吗? Change it as above and you're good to go. 如上更改它,您就很好了。
In this case also the other side of the relationship should be changed to hasMany
(although it is not a problem in the code you pasted): 在这种情况下,关系的另一端也应更改为
hasMany
(尽管在您粘贴的代码中这不是问题):
public function projects()
{
return $this->hasMany('Project');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.