php - Unexpected results in eloquent query -
i running following query in laravel code,
$organisations = organisation::with('projects', 'projects.clients') ->with('clients') ->get(); $organisations->load('users');
this loading in organisations table, , should in thoery load in related models expect object looks returned,
organisation { projects { clients { } } users { } }
basically organisation can have many projects, project can have 1 client, organisation can have many users , many clients.
what wanting query client data returned each project not getting that, getting following object returned.
[ { "id": "114", "name": "xxxxxxx", "slug": "xxxxxxx", "information": "", "type": "organisation", "notifications": "0", "add_all": "0", "created_at": "2014-12-16 10:16:03", "updated_at": "2014-12-16 10:16:03", "clients": [ { "id": "39", "name": "simon's test", "information": "", "address": null, "website": null, "email": null, "phone": null, "type": "client", "add_all": "0", "created_at": "2014-12-16 10:16:20", "updated_at": "2014-12-16 10:16:20", "user_id": "1", "owner_id": "114", "projects": [ { "id": "56", "name": "ios application", "description": "an ios application available on iphone , ipad. change way manage projects.", "total_cost": "5000.00", "start_date": "2014-01-01", "finish_date": "2014-12-10", "status": "1", "sales_person": null, "project_manager": null, "client_id": "39", "organisation_id": "114", "owner_id": "114", "user_id": "1", "created_at": null, "updated_at": "2014-12-15 13:20:07" }, { "id": "57", "name": "android application", "description": "an android application run on android phone, upto version.", "total_cost": "6500.00", "start_date": "2015-01-31", "finish_date": "2015-03-19", "status": "2", "sales_person": null, "project_manager": null, "client_id": "39", "organisation_id": "114", "owner_id": "114", "user_id": "1", "created_at": null, "updated_at": "2014-12-11 15:01:15" }, { "id": "58", "name": "java application", "description": "a windows phone application, released small market share market has.", "total_cost": "7500.00", "start_date": "2014-12-12", "finish_date": "2014-12-31", "status": "3", "sales_person": null, "project_manager": null, "client_id": "39", "organisation_id": "114", "owner_id": "114", "user_id": "1", "created_at": null, "updated_at": "2014-12-11 15:01:15" }, { "id": "59", "name": "osx application beta", "description": "a desktop application mac. more feature rich native ios apps, more web application desktop.", "total_cost": "20000.00", "start_date": "2014-11-20", "finish_date": "2014-12-19", "status": "2", "sales_person": null, "project_manager": null, "client_id": "39", "organisation_id": "114", "owner_id": "114", "user_id": "1", "created_at": null, "updated_at": "2014-11-28 16:01:41" } ], "users": [] } ], "projects": [ { "id": "56", "name": "ios application", "description": "an ios application available on iphone , ipad. change way manage projects.", "total_cost": "5000.00", "start_date": "2014-01-01", "finish_date": "2014-12-10", "status": "1", "sales_person": null, "project_manager": null, "client_id": "39", "organisation_id": "114", "owner_id": "114", "user_id": "1", "created_at": null, "updated_at": "2014-12-15 13:20:07", "pivot": { "organisation_id": "114", "project_id": "56" } }, { "id": "57", "name": "android application", "description": "an android application run on android phone, upto version.", "total_cost": "6500.00", "start_date": "2015-01-31", "finish_date": "2015-03-19", "status": "2", "sales_person": null, "project_manager": null, "client_id": "39", "organisation_id": "114", "owner_id": "114", "user_id": "1", "created_at": null, "updated_at": "2014-12-11 15:01:15", "pivot": { "organisation_id": "114", "project_id": "57" } }, { "id": "58", "name": "java application", "description": "a windows phone application, released small market share market has.", "total_cost": "7500.00", "start_date": "2014-12-12", "finish_date": "2014-12-31", "status": "3", "sales_person": null, "project_manager": null, "client_id": "39", "organisation_id": "114", "owner_id": "114", "user_id": "1", "created_at": null, "updated_at": "2014-12-11 15:01:15", "pivot": { "organisation_id": "114", "project_id": "58" } }, { "id": "59", "name": "osx application beta", "description": "a desktop application mac. more feature rich native ios apps, more web application desktop.", "total_cost": "20000.00", "start_date": "2014-11-20", "finish_date": "2014-12-19", "status": "2", "sales_person": null, "project_manager": null, "client_id": "39", "organisation_id": "114", "owner_id": "114", "user_id": "1", "created_at": null, "updated_at": "2014-11-28 16:01:41", "pivot": { "organisation_id": "114", "project_id": "59" } } ], "users": [ { "id": "1", "email": "simon@pikcells.com", "first_name": "simon", "last_name": "ainley", "display_name": "simonainley", "initials": "sa", "remember_me": null, "active": "1", "invite_code": null, "forgotten_code": null, "login_type": "normal", "api_token": null, "created_at": "-0001-11-30 00:00:00", "updated_at": "2014-12-12 11:53:53", "deleted_at": null, "pivot": { "organisation_id": "114", "user_id": "1", "is_admin": "1" } } ] } ]
in object above can see organisations projects , clients returned me, clients have nested projects, i.e projects related too, return projects nested clients related possible?
one note naming. if projects can have 1 client relationship better off if called "client" instead of "clients".
project model
//client relationship in project model client() { $this->belongsto('client') }
this way more readable , can call this
organisation::with('projects.client', 'clients', 'users')->get()
you need relationships projects, clients , users in organisation model.
check eloquent documentation more info relationships
Comments
Post a Comment