douou1872
2015-02-06 17:42
浏览 101
已采纳

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

I have relationship that is setup like this,

Organisation.php

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

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

multiple 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.

Why would it not be doing?

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dpglo66848 2015-02-06 19:09
    已采纳

    It's belongsTo:

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

    belongsToMany is a m-m relation with pivot table, which is not the case here, since you have organisation_id, right? 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):

    public function projects()
    {
        return $this->hasMany('Project');
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题