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

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');
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求解 yolo算法问题
  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝