Laravel Eloquent ->load() not returning expected results - php

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\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"
}
}
]
}
]
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?

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');
}

Related

Grouping, counting and sorting collection takes too long. How can I improve my Laravel code?

In my app, there are several businesses, each business can have multiple orders, and each order can have various products. To get all business orders, including products (relation), I use $orders = $business->orders()->with('products')->get(); I need to calculate the quantity how often the product is ordered, in other words, the number of times I see each product in my orders.
So after get(), I have the following results.
[
{
"id": "94567311-e554-4265-8753-db5d021f0544",
"business_id": "8ba6c772-8c2d-4f3b-a7d6-3f1f0648fa77",
"user_id": null,
"business_customer_id": null,
"customer_name": " Nitin",
"customer_email": "nitin#hit-pay.com",
"customer_phone_number": "123123",
"customer_street": "#16-31, 5A LORONG HOW SUN, Bartley Residences, SINGAPORE 536563",
"customer_city": "Singapore",
"customer_state": "Singapore",
"customer_postal_code": "536563",
"customer_country": "sg",
"customer_pickup": 0,
"channel": "store_checkout",
"currency": "sgd",
"automatic_discount_name": "amount discount",
"automatic_discount_amount": 500,
"line_item_price": 3000,
"line_item_discount_amount": 0,
"line_item_tax_amount": 0,
"automatic_discount_reason": null,
"additional_discount_amount": 0,
"business_shipping_id": "937d5b59-b449-4901-a685-e208460f426c",
"shipping_method": "Shipping Not Applicable",
"shipping_amount": 0,
"shipping_tax_name": null,
"shipping_tax_rate": "0.0000",
"shipping_tax_amount": 0,
"slot_date": null,
"slot_time": null,
"amount": 2500,
"coupon_amount": 0,
"tax_setting_id": null,
"reference": null,
"messages": null,
"remark": null,
"status": "requires_business_action",
"executor_id": null,
"request_ip_address": "116.86.78.44",
"request_user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15",
"request_method": "post",
"request_url": "https://staging.myshop.sg/8ba6c772-8c2d-4f3b-a7d6-3f1f0648fa77/checkout",
"request_country": null,
"request_data": {
"device": {
"name": "Macintosh",
"type": "computer"
},
"browser": {
"name": "Safari",
"version": "14.1.1"
},
"platform": {
"name": "OS X",
"version": "10_15_7"
},
"referrer": "https://staging.myshop.sg/8ba6c772-8c2d-4f3b-a7d6-3f1f0648fa77/checkout",
"requests": {
"email": "nitin#hit-pay.com",
"discount": {
"name": "amount discount",
"amount": 500
},
"shipping": {
"option": "937d5b59-b449-4901-a685-e208460f426c",
"address": {
"city": "Singapore",
"state": "Singapore",
"street": "#16-31, 5A LORONG HOW SUN, Bartley Residences, SINGAPORE 536563",
"country": "sg",
"postal_code": "536563"
}
},
"last_name": "Nitin",
"phone_number": "123123"
}
},
"created_at": "2021-09-07 12:49:46",
"updated_at": "2021-09-07 12:50:07",
"expires_at": null,
"closed_at": null,
"products_count": 2,
"products": [
{
"id": "94567311-e669-4943-b90e-537669099bc2",
"business_order_id": "94567311-e554-4265-8753-db5d021f0544",
"business_product_id": "92df0ca8-762d-4caf-a3af-ea0359906338",
"stock_keeping_unit": null,
"name": "Black Tote Bag",
"description": "Small",
"weight": null,
"length": null,
"width": null,
"depth": null,
"variation_key_1": "Size",
"variation_value_1": "Small",
"variation_key_2": null,
"variation_value_2": null,
"variation_key_3": null,
"variation_value_3": null,
"quantity": 1,
"tax_name": null,
"tax_rate": "0.0000",
"unit_price": 1500,
"tax_amount": 0,
"discount_amount": 0,
"price": 1500,
"remark": null,
"business_image_id": "92516d93-e06c-4718-b49a-8d63844ce5e5",
"created_at": "2021-09-07 12:49:46",
"updated_at": "2021-09-07 12:49:46",
"deleted_at": null
},
{
"id": "94567311-e6bb-48b4-8bdf-ad282dcc6d72",
"business_order_id": "94567311-e554-4265-8753-db5d021f0544",
"business_product_id": "92df0ca8-7781-4d9a-859e-8296669a9926",
"stock_keeping_unit": null,
"name": "Black Tote Bag",
"description": "Medium",
"weight": null,
"length": null,
"width": null,
"depth": null,
"variation_key_1": "Size",
"variation_value_1": "Medium",
"variation_key_2": null,
"variation_value_2": null,
"variation_key_3": null,
"variation_value_3": null,
"quantity": 1,
"tax_name": null,
"tax_rate": "0.0000",
"unit_price": 1500,
"tax_amount": 0,
"discount_amount": 0,
"price": 1500,
"remark": null,
"business_image_id": "92516d93-e06c-4718-b49a-8d63844ce5e5",
"created_at": "2021-09-07 12:49:46",
"updated_at": "2021-09-07 12:49:46",
"deleted_at": null
}
]
},
{
"id": "94606929-ab27-49c1-b8da-21cefee04e7a",
"business_id": "8ba6c772-8c2d-4f3b-a7d6-3f1f0648fa77",
"user_id": null,
"business_customer_id": null,
"customer_name": " H",
"customer_email": "test#gmail.com",
"customer_phone_number": "0290839918",
"customer_street": "Dn",
"customer_city": "Dn",
"customer_state": "Dn",
"customer_postal_code": "550000",
"customer_country": "sg",
"customer_pickup": 0,
"channel": "store_checkout",
"currency": "sgd",
"automatic_discount_name": "amount discount",
"automatic_discount_amount": 500,
"line_item_price": 3500,
"line_item_discount_amount": 0,
"line_item_tax_amount": 0,
"automatic_discount_reason": null,
"additional_discount_amount": 0,
"business_shipping_id": "937d5b59-b449-4901-a685-e208460f426c",
"shipping_method": "Shipping Not Applicable",
"shipping_amount": 0,
"shipping_tax_name": null,
"shipping_tax_rate": "0.0000",
"shipping_tax_amount": 0,
"slot_date": null,
"slot_time": null,
"amount": 3000,
"coupon_amount": 0,
"tax_setting_id": null,
"reference": null,
"messages": null,
"remark": null,
"status": "requires_business_action",
"executor_id": null,
"request_ip_address": "14.185.167.6",
"request_user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:92.0) Gecko/20100101 Firefox/92.0",
"request_method": "post",
"request_url": "https://staging.myshop.sg/8ba6c772-8c2d-4f3b-a7d6-3f1f0648fa77/checkout",
"request_country": null,
"request_data": {
"device": {
"type": "computer"
},
"browser": {
"name": "Firefox",
"version": "92.0"
},
"platform": {
"name": "Windows",
"version": "10.0"
},
"referrer": "https://staging.myshop.sg/8ba6c772-8c2d-4f3b-a7d6-3f1f0648fa77/checkout",
"requests": {
"email": "test#gmail.com",
"discount": {
"name": "amount discount",
"amount": 500
},
"shipping": {
"option": "937d5b59-b449-4901-a685-e208460f426c",
"address": {
"city": "Dn",
"state": "Dn",
"street": "Dn",
"country": "sg",
"postal_code": "550000"
}
},
"last_name": "H",
"phone_number": "0290839918"
}
},
"created_at": "2021-09-12 11:40:21",
"updated_at": "2021-09-12 11:41:38",
"expires_at": null,
"closed_at": null,
"products_count": 2,
"products": [
{
"id": "94606929-ad05-4c67-9491-644e8d3b434b",
"business_order_id": "94606929-ab27-49c1-b8da-21cefee04e7a",
"business_product_id": "92df0ca8-762d-4caf-a3af-ea0359906338",
"stock_keeping_unit": null,
"name": "Black Tote Bag",
"description": "Small",
"weight": null,
"length": null,
"width": null,
"depth": null,
"variation_key_1": "Size",
"variation_value_1": "Small",
"variation_key_2": null,
"variation_value_2": null,
"variation_key_3": null,
"variation_value_3": null,
"quantity": 1,
"tax_name": null,
"tax_rate": "0.0000",
"unit_price": 1500,
"tax_amount": 0,
"discount_amount": 0,
"price": 1500,
"remark": null,
"business_image_id": "92516d93-e06c-4718-b49a-8d63844ce5e5",
"created_at": "2021-09-12 11:40:21",
"updated_at": "2021-09-12 11:40:21",
"deleted_at": null
},
{
"id": "94606929-ad66-4a69-93ae-1e291c2d7e53",
"business_order_id": "94606929-ab27-49c1-b8da-21cefee04e7a",
"business_product_id": "93253bf2-ce63-4a9a-b4a6-8ecaff439028",
"stock_keeping_unit": null,
"name": "Bynd Artisan Tote Bag Duplicate",
"description": "Blue / Small",
"weight": null,
"length": null,
"width": null,
"depth": null,
"variation_key_1": "Color",
"variation_value_1": "Blue",
"variation_key_2": "Size",
"variation_value_2": null,
"variation_key_3": null,
"variation_value_3": null,
"quantity": 1,
"tax_name": null,
"tax_rate": "0.0000",
"unit_price": 2000,
"tax_amount": 0,
"discount_amount": 0,
"price": 2000,
"remark": null,
"business_image_id": "93253bf2-e5c2-4a22-8605-81c245bcf86b",
"created_at": "2021-09-12 11:40:21",
"updated_at": "2021-09-12 11:40:21",
"deleted_at": null
}
]
},
{
"id": "94606a50-2a40-476e-bd71-50c096270174",
"business_id": "8ba6c772-8c2d-4f3b-a7d6-3f1f0648fa77",
"user_id": null,
"business_customer_id": null,
"customer_name": " nitin",
"customer_email": "nitin#hit-pay.com",
"customer_phone_number": "9000123123",
"customer_street": "test",
"customer_city": "sg",
"customer_state": "singapore",
"customer_postal_code": "600123",
"customer_country": "sg",
"customer_pickup": 0,
"channel": "store_checkout",
"currency": "sgd",
"automatic_discount_name": "% discount",
"automatic_discount_amount": 200,
"line_item_price": 2000,
"line_item_discount_amount": 0,
"line_item_tax_amount": 0,
"automatic_discount_reason": null,
"additional_discount_amount": 0,
"business_shipping_id": "937d5b59-b449-4901-a685-e208460f426c",
"shipping_method": "Shipping Not Applicable",
"shipping_amount": 0,
"shipping_tax_name": null,
"shipping_tax_rate": "0.0000",
"shipping_tax_amount": 0,
"slot_date": null,
"slot_time": null,
"amount": 1800,
"coupon_amount": 0,
"tax_setting_id": null,
"reference": null,
"messages": null,
"remark": "test",
"status": "requires_business_action",
"executor_id": null,
"request_ip_address": "116.86.78.44",
"request_user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15",
"request_method": "post",
"request_url": "https://staging.myshop.sg/8ba6c772-8c2d-4f3b-a7d6-3f1f0648fa77/checkout",
"request_country": null,
"request_data": {
"device": {
"name": "Macintosh",
"type": "computer"
},
"browser": {
"name": "Safari",
"version": "14.1.1"
},
"platform": {
"name": "OS X",
"version": "10_15_7"
},
"referrer": "https://staging.myshop.sg/8ba6c772-8c2d-4f3b-a7d6-3f1f0648fa77/checkout",
"requests": {
"email": "nitin#hit-pay.com",
"remark": "test",
"discount": {
"name": "% discount",
"amount": 200
},
"shipping": {
"option": "937d5b59-b449-4901-a685-e208460f426c",
"address": {
"city": "sg",
"state": "singapore",
"street": "test",
"country": "sg",
"postal_code": "600123"
}
},
"last_name": "nitin",
"phone_number": "9000123123"
}
},
"created_at": "2021-09-12 11:43:34",
"updated_at": "2021-09-12 11:44:02",
"expires_at": null,
"closed_at": null,
"products_count": 1,
"products": [
{
"id": "94606a50-2bac-48b8-860f-eebec27aff8d",
"business_order_id": "94606a50-2a40-476e-bd71-50c096270174",
"business_product_id": "93253bf2-ce63-4a9a-b4a6-8ecaff439028",
"stock_keeping_unit": null,
"name": "Bynd Artisan Tote Bag Duplicate",
"description": "Blue / Small",
"weight": null,
"length": null,
"width": null,
"depth": null,
"variation_key_1": "Color",
"variation_value_1": "Blue",
"variation_key_2": "Size",
"variation_value_2": null,
"variation_key_3": null,
"variation_value_3": null,
"quantity": 1,
"tax_name": null,
"tax_rate": "0.0000",
"unit_price": 2000,
"tax_amount": 0,
"discount_amount": 0,
"price": 2000,
"remark": null,
"business_image_id": "93253bf2-e5c2-4a22-8605-81c245bcf86b",
"created_at": "2021-09-12 11:43:34",
"updated_at": "2021-09-12 11:43:34",
"deleted_at": null
}
]
},
{
"id": "9466c9d9-2a57-4551-b36b-294b64fb9fe4",
"business_id": "8ba6c772-8c2d-4f3b-a7d6-3f1f0648fa77",
"user_id": null,
"business_customer_id": null,
"customer_name": " Test",
"customer_email": "test#gmail.com",
"customer_phone_number": "092028028",
"customer_street": "DN",
"customer_city": "DN",
"customer_state": "DN",
"customer_postal_code": "550000",
"customer_country": "sg",
"customer_pickup": 0,
"channel": "store_checkout",
"currency": "sgd",
"automatic_discount_name": "% discount",
"automatic_discount_amount": 200,
"line_item_price": 2000,
"line_item_discount_amount": 0,
"line_item_tax_amount": 0,
"automatic_discount_reason": null,
"additional_discount_amount": 0,
"business_shipping_id": "937d5b59-b449-4901-a685-e208460f426c",
"shipping_method": "Shipping Not Applicable",
"shipping_amount": 0,
"shipping_tax_name": null,
"shipping_tax_rate": "0.0000",
"shipping_tax_amount": 0,
"slot_date": null,
"slot_time": null,
"amount": 1800,
"coupon_amount": 0,
"tax_setting_id": null,
"reference": null,
"messages": null,
"remark": null,
"status": "requires_business_action",
"executor_id": null,
"request_ip_address": "14.185.201.146",
"request_user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36",
"request_method": "post",
"request_url": "https://staging.myshop.sg/8ba6c772-8c2d-4f3b-a7d6-3f1f0648fa77/checkout",
"request_country": null,
"request_data": {
"device": {
"name": "WebKit",
"type": "computer"
},
"browser": {
"name": "Chrome",
"version": "93.0.4577.63"
},
"platform": {
"name": "Windows",
"version": "10.0"
},
"referrer": "https://staging.myshop.sg/8ba6c772-8c2d-4f3b-a7d6-3f1f0648fa77/checkout",
"requests": {
"email": "test#gmail.com",
"discount": {
"name": "% discount",
"amount": 200
},
"shipping": {
"option": "937d5b59-b449-4901-a685-e208460f426c",
"address": {
"city": "DN",
"state": "DN",
"street": "DN",
"country": "sg",
"postal_code": "550000"
}
},
"last_name": "Test",
"phone_number": "092028028"
}
},
"created_at": "2021-09-15 15:45:40",
"updated_at": "2021-09-15 15:46:08",
"expires_at": null,
"closed_at": null,
"products_count": 1,
"products": [
{
"id": "9466c9d9-3131-43f7-99f3-c41ccd3308d1",
"business_order_id": "9466c9d9-2a57-4551-b36b-294b64fb9fe4",
"business_product_id": "93253bf2-ce63-4a9a-b4a6-8ecaff439028",
"stock_keeping_unit": null,
"name": "Bynd Artisan Tote Bag Duplicate",
"description": "Blue / Small",
"weight": null,
"length": null,
"width": null,
"depth": null,
"variation_key_1": "Color",
"variation_value_1": "Blue",
"variation_key_2": "Size",
"variation_value_2": null,
"variation_key_3": null,
"variation_value_3": null,
"quantity": 1,
"tax_name": null,
"tax_rate": "0.0000",
"unit_price": 2000,
"tax_amount": 0,
"discount_amount": 0,
"price": 2000,
"remark": null,
"business_image_id": "93253bf2-e5c2-4a22-8605-81c245bcf86b",
"created_at": "2021-09-15 15:45:40",
"updated_at": "2021-09-15 15:45:40",
"deleted_at": null
}
]
}
]
Using the following code, I achieved exactly what I wanted:
$orders = $business->orders()->with('products')->get();
foreach ($orders as $order){
foreach ($order->products as $product)
$subset[] = $product;
}
$groupedOrderedProducts = collect($subset)->groupBy('business_product_id');
$topProducts = $groupedOrderedProducts->map(function ($product){
return ['product' => $product->first(),'count'=>$product->count()];
})->sortByDesc('count');
Results:
{
"93253bf2-ce63-4a9a-b4a6-8ecaff439028": {
"product": {
"id": "94606929-ad66-4a69-93ae-1e291c2d7e53",
"business_order_id": "94606929-ab27-49c1-b8da-21cefee04e7a",
"business_product_id": "93253bf2-ce63-4a9a-b4a6-8ecaff439028",
"stock_keeping_unit": null,
"name": "Bynd Artisan Tote Bag Duplicate",
"description": "Blue / Small",
"weight": null,
"length": null,
"width": null,
"depth": null,
"variation_key_1": "Color",
"variation_value_1": "Blue",
"variation_key_2": "Size",
"variation_value_2": null,
"variation_key_3": null,
"variation_value_3": null,
"quantity": 1,
"tax_name": null,
"tax_rate": "0.0000",
"unit_price": 2000,
"tax_amount": 0,
"discount_amount": 0,
"price": 2000,
"remark": null,
"business_image_id": "93253bf2-e5c2-4a22-8605-81c245bcf86b",
"created_at": "2021-09-12 11:40:21",
"updated_at": "2021-09-12 11:40:21",
"deleted_at": null
},
"count": 4
},
"92df0ca8-762d-4caf-a3af-ea0359906338": {
"product": {
"id": "94567311-e669-4943-b90e-537669099bc2",
"business_order_id": "94567311-e554-4265-8753-db5d021f0544",
"business_product_id": "92df0ca8-762d-4caf-a3af-ea0359906338",
"stock_keeping_unit": null,
"name": "Black Tote Bag",
"description": "Small",
"weight": null,
"length": null,
"width": null,
"depth": null,
"variation_key_1": "Size",
"variation_value_1": "Small",
"variation_key_2": null,
"variation_value_2": null,
"variation_key_3": null,
"variation_value_3": null,
"quantity": 1,
"tax_name": null,
"tax_rate": "0.0000",
"unit_price": 1500,
"tax_amount": 0,
"discount_amount": 0,
"price": 1500,
"remark": null,
"business_image_id": "92516d93-e06c-4718-b49a-8d63844ce5e5",
"created_at": "2021-09-07 12:49:46",
"updated_at": "2021-09-07 12:49:46",
"deleted_at": null
},
"count": 3
},
"92df0ca8-7781-4d9a-859e-8296669a9926": {
"product": {
"id": "94567311-e6bb-48b4-8bdf-ad282dcc6d72",
"business_order_id": "94567311-e554-4265-8753-db5d021f0544",
"business_product_id": "92df0ca8-7781-4d9a-859e-8296669a9926",
"stock_keeping_unit": null,
"name": "Black Tote Bag",
"description": "Medium",
"weight": null,
"length": null,
"width": null,
"depth": null,
"variation_key_1": "Size",
"variation_value_1": "Medium",
"variation_key_2": null,
"variation_value_2": null,
"variation_key_3": null,
"variation_value_3": null,
"quantity": 1,
"tax_name": null,
"tax_rate": "0.0000",
"unit_price": 1500,
"tax_amount": 0,
"discount_amount": 0,
"price": 1500,
"remark": null,
"business_image_id": "92516d93-e06c-4718-b49a-8d63844ce5e5",
"created_at": "2021-09-07 12:49:46",
"updated_at": "2021-09-07 12:49:46",
"deleted_at": null
},
"count": 1
}
}
I feel that my code is not the best solution. How can I improve it?
You can define a hasManyThrough relationship for business and products, that way you can select the products of the business directly and group them.
$groupedOrderedProducts = $business->products()->groupBy('business_product_id')->get();

Laravel: How I can get top products from my orders?

I need to get the products that are ordered most often. I can get orders with ordered products this way:
$business->orders()->with('products')->get();
It returns the next collection (I just show you one order):
[
{
"id": "94567311-e554-4265-8753-db5d021f0544",
"business_id": "8ba6c772-8c2d-4f3b-a7d6-3f1f0648fa77",
"user_id": null,
"business_customer_id": null,
"customer_name": " Alona",
"customer_email": "alona#fff.ff",
"customer_phone_number": "123123",
"customer_street": "#16-31, 5A LORONG HOW SUN, Bartley Residences, SINGAPORE 536563",
"customer_city": "Singapore",
"customer_state": "Singapore",
"customer_postal_code": "536563",
"customer_country": "sg",
"customer_pickup": 0,
"channel": "store_checkout",
"currency": "sgd",
"automatic_discount_name": "amount discount",
"automatic_discount_amount": 500,
"line_item_price": 3000,
"line_item_discount_amount": 0,
"line_item_tax_amount": 0,
"automatic_discount_reason": null,
"additional_discount_amount": 0,
"business_shipping_id": "937d5b59-b449-4901-a685-e208460f426c",
"shipping_method": "Shipping Not Applicable",
"shipping_amount": 0,
"shipping_tax_name": null,
"shipping_tax_rate": "0.0000",
"shipping_tax_amount": 0,
"slot_date": null,
"slot_time": null,
"amount": 2500,
"coupon_amount": 0,
"tax_setting_id": null,
"reference": null,
"messages": null,
"remark": null,
"status": "requires_business_action",
"executor_id": null,
"request_ip_address": "116.86.78.44",
"request_user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.1.1 Safari/605.1.15",
"request_method": "post",
"request_url": "",
"request_country": null,
"request_data": {
"device": {
"name": "Macintosh",
"type": "computer"
},
"browser": {
"name": "Safari",
"version": "14.1.1"
},
"platform": {
"name": "OS X",
"version": "10_15_7"
},
"referrer": "",
"requests": {
"email": "alona#fd.com",
"discount": {
"name": "amount discount",
"amount": 500
},
"shipping": {
"option": "937d5b59-b449-4901-a685-e208460f426c",
"address": {
"city": "Singapore",
"state": "Singapore",
"street": "#16-31, 5A LORONG HOW SUN, Bartley Residences, SINGAPORE 536563",
"country": "sg",
"postal_code": "536563"
}
},
"last_name": "alona",
"phone_number": "123123"
}
},
"created_at": "2021-09-07 12:49:46",
"updated_at": "2021-09-07 12:50:07",
"expires_at": null,
"closed_at": null,
"products_count": 2,
"products": [
{
"id": "94567311-e669-4943-b90e-537669099bc2",
"business_order_id": "94567311-e554-4265-8753-db5d021f0544",
"business_product_id": "92df0ca8-762d-4caf-a3af-ea0359906338",
"stock_keeping_unit": null,
"name": "Black Tote Bag",
"description": "Small",
"weight": null,
"length": null,
"width": null,
"depth": null,
"variation_key_1": "Size",
"variation_value_1": "Small",
"variation_key_2": null,
"variation_value_2": null,
"variation_key_3": null,
"variation_value_3": null,
"quantity": 1,
"tax_name": null,
"tax_rate": "0.0000",
"unit_price": 1500,
"tax_amount": 0,
"discount_amount": 0,
"price": 1500,
"remark": null,
"business_image_id": "92516d93-e06c-4718-b49a-8d63844ce5e5",
"created_at": "2021-09-07 12:49:46",
"updated_at": "2021-09-07 12:49:46",
"deleted_at": null
},
{
"id": "94567311-e6bb-48b4-8bdf-ad282dcc6d72",
"business_order_id": "94567311-e554-4265-8753-db5d021f0544",
"business_product_id": "92df0ca8-7781-4d9a-859e-8296669a9926",
"stock_keeping_unit": null,
"name": "Black Tote Bag",
"description": "Medium",
"weight": null,
"length": null,
"width": null,
"depth": null,
"variation_key_1": "Size",
"variation_value_1": "Medium",
"variation_key_2": null,
"variation_value_2": null,
"variation_key_3": null,
"variation_value_3": null,
"quantity": 1,
"tax_name": null,
"tax_rate": "0.0000",
"unit_price": 1500,
"tax_amount": 0,
"discount_amount": 0,
"price": 1500,
"remark": null,
"business_image_id": "92516d93-e06c-4718-b49a-8d63844ce5e5",
"created_at": "2021-09-07 12:49:46",
"updated_at": "2021-09-07 12:49:46",
"deleted_at": null
}
]
},
....
]
Each order can have multiple products. So I need somehow to get a count of how many times each product was ordered. Something like this:
[
'count' => 37
'product' => {
"id": "94567311-e669-4943-b90e-537669099bc2",
"business_order_id": "94567311-e554-4265-8753-db5d021f0544",
"business_product_id": "92df0ca8-762d-4caf-a3af-ea0359906338",
"stock_keeping_unit": null,
"name": "Black Tote Bag",
"description": "Small",
"weight": null,
"length": null,
"width": null,
"depth": null,
"variation_key_1": "Size",
"variation_value_1": "Small",
"variation_key_2": null,
"variation_value_2": null,
"variation_key_3": null,
"variation_value_3": null,
"quantity": 1,
"tax_name": null,
"tax_rate": "0.0000",
"unit_price": 1500,
"tax_amount": 0,
"discount_amount": 0,
"price": 1500,
"remark": null,
"business_image_id": "92516d93-e06c-4718-b49a-8d63844ce5e5",
"created_at": "2021-09-07 12:49:46",
"updated_at": "2021-09-07 12:49:46",
"deleted_at": null
},
]
Any advice will be helpful. Thanks all!
You can count a relation, so assuming you also defined the relation on products. This should be a good start:
Products::withCount('orders')->orderBy('orders_count', 'desc')->paginate(10);
$business->orders()->with(['products'=>function($qu)
{
$qu->orderBy('orders_count', 'desc');
}])->get();
Try This

Laravel I can't retrieve requester data using with

I am using this package
https://github.com/renoki-co/befriended
My issue is trying to get all my friends posts with my post including user's data, but the problem is the requester data is null.
$user = $request->user();
$followersPost = UserPost::with(['user' => function ($query) use ($user) {
$query->filterFollowingsOf($user);
}])->latest()->get();
return $followersPost;
Response
[
{
"id": 35,
"post_body": "The Requster Post",
"location_id": "4b0da1acf964a520b94c23e3",
"location_title": "Apple Infinite Loop",
"location_icon": "https://ss3.4sqi.net/img/categories_v2/shops/technology_bg_44.png",
"location_coordinate": "{"latitude":37.33167558501772,"longitude":-122.030189037323,"latitudeDelta":0.001,"longitudeDelta":0.001}",
"location_vicinity": "Cupertino - 1 Infinite Loop",
"user_id": 1,
"viewer_ids": null,
"created_at": "2020-12-06T18:08:58.000000Z",
"updated_at": "2020-12-06T18:08:58.000000Z",
"images": [
],
"musics": [
],
"movies": [
],
"emojs": [
],
"user": null
},
{
"id": 34,
"post_body": "Test Post",
"location_id": "4b0da1acf964a520b94c23e3",
"location_title": "Apple Infinite Loop",
"location_icon": "https://ss3.4sqi.net/img/categories_v2/shops/technology_bg_44.png",
"location_coordinate": "{"latitude":37.33167558501772,"longitude":-122.030189037323,"latitudeDelta":0.001,"longitudeDelta":0.001}",
"location_vicinity": "Cupertino - 1 Infinite Loop",
"user_id": 2,
"viewer_ids": null,
"created_at": "2020-12-06T18:08:14.000000Z",
"updated_at": "2020-12-06T18:08:14.000000Z",
"images": [
],
"musics": [
],
"movies": [
],
"emojs": [
],
"user": {
"id": 2,
"_id": "dfa19aa6-510b-4aaf-8475-4386da9de3d0",
"name": "Saleh",
"username": "saleh",
"email": "saleh#gmail.com",
"email_verified_at": null,
"avatar": "http://backend.mf.sa/storage/avatars/1607128041.jpg",
"location": null,
"gender": "male",
"bio": null,
"birthday": null,
"phone": null,
"created_at": "2020-12-05T00:27:02.000000Z",
"updated_at": "2020-12-05T00:27:21.000000Z"
}
},
{
"id": 24,
"post_body": null,
"location_id": null,
"location_title": null,
"location_icon": null,
"location_coordinate": null,
"location_vicinity": null,
"user_id": 3,
"viewer_ids": null,
"created_at": "2020-12-06T01:53:18.000000Z",
"updated_at": "2020-12-06T01:53:18.000000Z",
"images": [
],
"musics": [
{
"id": 4,
"post_id": 24,
"song_name": "Confetti",
"song_preview": "https://cdns-preview-6.dzcdn.net/stream/c-6eb643a05083c023f874f13739fc7c87-3.mp3",
"song_picture": "https://api.deezer.com/album/183603832/image",
"song_artist": "Little Mix",
"created_at": "2020-12-06T01:53:18.000000Z",
"updated_at": "2020-12-06T01:53:18.000000Z"
}
],
"movies": [
],
"emojs": [
{
"id": 16,
"post_id": 24,
"user_id": 3,
"emojy": "015-smile-1",
"created_at": "2020-12-06T01:59:15.000000Z",
"updated_at": "2020-12-06T01:59:15.000000Z",
"user": {
"id": 3,
"_id": "af65864b-f18a-4609-91db-336acbce1486",
"name": "Ali",
"username": "ali2323",
"email": "ali#gmail.com",
"email_verified_at": null,
"avatar": null,
"location": null,
"gender": "male",
"bio": null,
"birthday": null,
"phone": null,
"created_at": "2020-12-05T04:05:50.000000Z",
"updated_at": "2020-12-05T04:05:50.000000Z"
}
},
{
"id": 17,
"post_id": 24,
"user_id": 1,
"emojy": "089-broken-heart",
"created_at": "2020-12-06T16:58:07.000000Z",
"updated_at": "2020-12-06T16:58:24.000000Z",
"user": {
"id": 1,
"_id": "737ba284-424d-4b0d-8a56-2e845603f2ba",
"name": "Mohammed",
"username": "mohammed",
"email": "h4ck3r.x0#gmail.com",
"email_verified_at": null,
"avatar": "http://backend.mf.sa/storage/avatars/1607128001.jpg",
"location": null,
"gender": "male",
"bio": null,
"birthday": null,
"phone": null,
"created_at": "2020-12-05T00:26:09.000000Z",
"updated_at": "2020-12-05T00:26:41.000000Z"
}
},
{
"id": 19,
"post_id": 24,
"user_id": 2,
"emojy": "021-sad",
"created_at": "2020-12-06T16:58:36.000000Z",
"updated_at": "2020-12-06T16:58:36.000000Z",
"user": {
"id": 2,
"_id": "dfa19aa6-510b-4aaf-8475-4386da9de3d0",
"name": "Saleh",
"username": "saleh",
"email": "saleh#gmail.com",
"email_verified_at": null,
"avatar": "http://backend.mf.sa/storage/avatars/1607128041.jpg",
"location": null,
"gender": "male",
"bio": null,
"birthday": null,
"phone": null,
"created_at": "2020-12-05T00:27:02.000000Z",
"updated_at": "2020-12-05T00:27:21.000000Z"
}
}
],
"user": {
"id": 3,
"_id": "af65864b-f18a-4609-91db-336acbce1486",
"name": "Ali",
"username": "ali2323",
"email": "ali#gmail.com",
"email_verified_at": null,
"avatar": null,
"location": null,
"gender": "male",
"bio": null,
"birthday": null,
"phone": null,
"created_at": "2020-12-05T04:05:50.000000Z",
"updated_at": "2020-12-05T04:05:50.000000Z"
}
}
]
As you can see the first object in the array dosen't include the user data.
So what should i do $query->filterFollowingsOf($user); to include my data?
I think the with callback is used for modifying the query that selects the user data. Instead, try using a whereHas method that filters the related users.
$user = $request->user();
$followersPost = UserPost
::with('user')
->whereHas('user', function ($query) use ($user) {
$query->filterFollowingsOf($user);
})
->latest()
->get();
return $followersPost;
I'd also recommend using paginate() instead of get() for performance when there could be a significant number of posts.
Edit:
To get either your friend's posts or your posts, try:
$followersPost = UserPost
::with('user')
->whereHas('user', function ($query) use ($user) {
$query->filterFollowingsOf($user);
})
->orWhere('user_id', $user->id)
->latest()
->get();

How to sort the collection on custom condition in laravel 5.7?

I have an api ready that returns the following json response.
Response:
{
"success": true,
"conversation": [{
"id": 37,
"type": "1",
"name": "Sonali",
"created_at": "2019-02-18 13:26:10",
"updated_at": "2019-02-18 20:32:54",
"unread_count": 2,
"chat": {
"id": 357,
"conversation_id": "23",
"type": "text",
"sender_id": "37",
"receiver_id": "39",
"data": "{\"text\":\"hello....\"}",
"delivered_at": "2019-02-20 13:25:27",
"seen_at": null,
"created_at": "2019-02-20 13:25:10",
"updated_at": "2019-02-20 13:25:27",
"sender_name": "Sonali"
}
},
{
"id": 38,
"type": "1",
"name": "Raviraj",
"created_at": "2019-02-18 20:23:55",
"updated_at": "2019-02-18 20:32:47",
"unread_count": 0,
"chat": {
"id": 354,
"conversation_id": "22",
"type": "text",
"sender_id": "39",
"receiver_id": "38",
"data": "{\"text\":\"hey....\"}",
"delivered_at": null,
"seen_at": null,
"created_at": "2019-02-20 13:24:35",
"updated_at": "2019-02-20 13:24:35",
"sender_name": "Nitesh Kesarkar"
}
},
{
"id": 27,
"type": "1",
"name": "Rakesh",
"created_at": "2019-02-01 10:48:19",
"updated_at": "2019-02-07 11:35:10",
"unread_count": 1,
"chat": {
"id": 358,
"conversation_id": "21",
"type": "text",
"sender_id": "27",
"receiver_id": "39",
"data": "{\"text\":\"hello\"}",
"delivered_at": "2019-02-20 13:25:27",
"seen_at": null,
"created_at": "2019-02-20 13:25:24",
"updated_at": "2019-02-20 13:25:27",
"sender_name": "Rakesh Patil"
}
}
]
}
This response consists of list of users and their associated last chat message for currently logged in user. I want to sort this collection according to the latest message first. How can I do that?
Sorting should be based on the chat.created_at field.
Expected Result:
{
"success": true,
"conversation": [
{
"id": 27,
"type": "1",
"name": "Rakesh",
"created_at": "2019-02-01 10:48:19",
"updated_at": "2019-02-07 11:35:10",
"unread_count": 1,
"chat": {
"id": 358,
"conversation_id": "21",
"type": "text",
"sender_id": "27",
"receiver_id": "39",
"data": "{\"text\":\"hello\"}",
"delivered_at": "2019-02-20 13:25:27",
"seen_at": null,
"created_at": "2019-02-20 13:25:24",
"updated_at": "2019-02-20 13:25:27",
"sender_name": "Rakesh Patil"
}
},
{
"id": 37,
"type": "1",
"name": "Sonali",
"created_at": "2019-02-18 13:26:10",
"updated_at": "2019-02-18 20:32:54",
"unread_count": 2,
"chat": {
"id": 357,
"conversation_id": "23",
"type": "text",
"sender_id": "37",
"receiver_id": "39",
"data": "{\"text\":\"hello....\"}",
"delivered_at": "2019-02-20 13:25:27",
"seen_at": null,
"created_at": "2019-02-20 13:25:10",
"updated_at": "2019-02-20 13:25:27",
"sender_name": "Sonali"
}
},
{
"id": 38,
"type": "1",
"name": "Raviraj",
"created_at": "2019-02-18 20:23:55",
"updated_at": "2019-02-18 20:32:47",
"unread_count": 0,
"chat": {
"id": 354,
"conversation_id": "22",
"type": "text",
"sender_id": "39",
"receiver_id": "38",
"data": "{\"text\":\"hey....\"}",
"delivered_at": null,
"seen_at": null,
"created_at": "2019-02-20 13:24:35",
"updated_at": "2019-02-20 13:24:35",
"sender_name": "Nitesh Kesarkar"
}
}
]
}
UPDATE :
Adding these lines worked as expected. Thanks #JCode
$sorted = $chats->sortByDesc('chat.created_at');
$chats = $sorted->values()->all();
You are looking for the sortBy() method.
This will of course work if your JSON output can be turned into a collection via collect()—in a perfect scenario, your chat messages would be a model in Laravel.

Unexpected results in eloquent query

I am running the following query in my laravel code,
$organisations = Organisation::with('projects', 'projects.clients')
->with('clients')
->get();
$organisations->load('users');
This is loading in my organisations table, and should in thoery load in any related models as well so I would expect an object that looks like this to be returned,
organisation {
projects {
clients {
}
}
users {
}
}
Basically an organisation can have many projects, a project can have one client, an organisation can also have many users and many clients.
What I am wanting from my query is for the client data to returned with each project but I am not getting that, currently I am getting the 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 that will be available on iPhone and iPad. Something that will change the way we 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 that will run on any android phone, upto a certain 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, to be released for the small market share that 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 for the Mac. More feature rich than the native iOS apps, more like the web application for 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 that will be available on iPhone and iPad. Something that will change the way we 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 that will run on any android phone, upto a certain 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, to be released for the small market share that 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 for the Mac. More feature rich than the native iOS apps, more like the web application for 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 the object above you can see that I get the organisations projects and clients returned to me, but the clients have nested projects, i.e what projects they are related too, but I would like to return projects nested with the clients they are related too is this possible?
One note about the naming. If your projects can only have one client the relationship would better off if called "client" instead of "clients".
Project Model
//Client relationship in Project model
client()
{
$this->belongsTo('Client')
}
this way is more readable and you can call it like this
Organisation::with('projects.client', 'clients', 'users')->get()
you need the relationships projects, clients and users in the Organisation model.
Check the Eloquent documentation for more info about relationships
$organisations = Organisation::with(array('Projects'=>function($q){
$q->with('Client')
},'Clients','Users'))->get()
You can fetch expected output using above code
You have to define 3 relationships in Organization model : Projects,Clients,Users
You have to define 1 relationship in Project model : Client

Categories