0
我有關係,那就是建立這樣的,Laravel雄辯 - >負載()沒有返回預期的結果
Organisation.php
public function projects()
{
return $this->belongsToMany('Project');
}
Project.php
public function organisations()
{
return $this->belongsToMany('Organisation');
}
我運行以下查詢,
$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();
個我回來的結果是這樣的,
[
{
"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": "[email protected]",
"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": "[email protected]",
"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 protected]",
"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": "[email protected]",
"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": "[email protected]",
"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": "[email protected]",
"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": "[email protected]",
"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": "[email protected]",
"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"
}
}
]
}
]
正如你所看到的組織屬性是空的,但就是憑藉具有organisation_id它應該返回的組織。
爲什麼不這樣做?
關係的另一面可能需要改變('hasMany('Project')')。 – patricus 2015-02-06 20:52:52
@patricus True,已編輯。 – 2015-02-07 02:41:43