Laravel Eloquent - > load()没有返回预期的结果

时间:2015-02-06 17:42:03

标签: php laravel eloquent

我的关系设置如下,

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": "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"
                }
            }
        ]
    }

正如你所看到的那样,组织属性是空的,但是由于具有organisation_id的优点,它应该返回一个组织。

为什么不这样做?

1 个答案:

答案 0 :(得分:0)

这是belongsTo

public function organisation()
{
    return $this->belongsTo('Organisation');
}

belongsToMany是与数据透视表的m-m关系,这不是这里的情况,因为你有organisation_id,对吗?如上所述更改它,你很高兴。

在这种情况下,关系的另一面也应更改为hasMany(尽管在您粘贴的代码中不是问题):

public function projects()
{
    return $this->hasMany('Project');
}