Reputation: 37
I have some problems with Laravel Eloquent relationships. I'm trying to get more than 3 tables at once, and it's based on user, country, products, and sales, and the real problem is with sales.
At this point everything works besides getting sales, it's getting sales for every country for a specific product. So this is a current response that I'm getting:
[
{
"id":"479aa264-2d33-4f18-b59f-e388920069bc",
"user_id":"24a40f61-8ce9-4444-9b15-fb95c1eac24e",
"countries_id":"522173e2-14ce-4d44-b15e-be3be9b987d9",
"all_access":0,
"deleted_at":null,
"created_at":"2021-06-14T07:39:40.000000Z",
"updated_at":"2021-06-14T07:39:40.000000Z",
"country":{
"id":"522173e2-14ce-4d44-b15e-be3be9b987d9",
"name":"Lithuania",
"iso_alpha_2":"LT",
"iso_alpha_3":"LTU",
"native_name":"Lietuva",
"phone_code":370,
"capital":"Vilnius",
"continent":"Europe",
"languages":"{\"lit\":\"Lithuanian\"}",
"address_format":"{{recipient}} {{street}} {{postalcode}} {{city}} {{region}} {{country}}",
"flag":"https://cdn.countryflags.com/thumbs/lithuania/flag-400.png",
"time_zone":"[\"Europe\\/Vilnius\"]",
"locales":null,
"isActive":1,
"currency_id":"05f0951c-098a-48bc-8cd0-5daae248c236",
"created_at":"2021-05-21T07:44:21.000000Z",
"updated_at":"2021-05-21T07:49:18.000000Z",
"deleted_at":null
},
"products":[
{
"id":"83782cce-d512-4b29-8c14-ebf4c9eec050",
"user_id":"24a40f61-8ce9-4444-9b15-fb95c1eac24e",
"countries_id":"522173e2-14ce-4d44-b15e-be3be9b987d9",
"product_id":"ed198413-6551-48be-bf53-939dfb11a20a",
"deleted_at":null,
"created_at":"2021-06-14T07:39:40.000000Z",
"updated_at":"2021-06-14T07:39:40.000000Z",
"product":{
"id":"ed198413-6551-48be-bf53-939dfb11a20a",
"sku":"1011",
"slug":"product-1",
"type":"product",
"deleted_at":null,
"created_at":"2021-05-21T07:44:23.000000Z",
"updated_at":"2021-05-21T07:44:23.000000Z",
"sales":[
{
"id":"03e1d259-1a62-4a2a-889a-baa644214602",
"order_id":"9b0b6a57-a452-4663-9412-92aa1bb95a70",
"country_id":"f346faef-3614-4357-92d8-7c77047a0081",
"product_id":"ed198413-6551-48be-bf53-939dfb11a20a",
"package_id":null,
"package_info_id":"682678a2-9a10-4543-b497-834c8016e65d",
"qty":3,
"price_pcs":172.67,
"price":518,
"deleted_at":null,
"created_at":"2021-06-10T11:27:24.000000Z",
"updated_at":"2021-06-10T11:27:24.000000Z"
},
{
"id":"0a52afcf-1761-43cd-918e-b98c6ed617e7",
"order_id":"4264ad52-7d46-48ea-b1f0-91d55d36be3e",
"country_id":"d3e92cc0-91c0-478a-936a-8e06d4aecf66",
"product_id":"ed198413-6551-48be-bf53-939dfb11a20a",
"package_id":null,
"package_info_id":"dc47b8b4-6cdc-4497-9a57-10dfcc202ad0",
"qty":1,
"price_pcs":38.7,
"price":38.7,
"deleted_at":null,
"created_at":"2021-06-02T15:52:54.000000Z",
"updated_at":"2021-06-02T15:52:54.000000Z"
}
]
}
}
]
}
]
As you see, getting user, country info and the product is current, but product sales are not (different country). And here is what I'm doing in code.
First authenticating user and getting stats relation ship and products, like so:
$user = User::with('stats.products')->findOrFail(Auth::id());
And here is the model for user, it's returning countries that user has permission to see, with country info:
public function stats()
{
return $this->hasMany(UserCountries::class)->with(['country']);
}
And inside UserCountries I have relation products, it's getting product info and sales for a specific product:
public function products()
{
return $this->hasMany(Products::class, 'countries_id', 'countries_id')->where('user_id', Auth::id())->with(['product.sales']);
}
So product model is basic hasOne relationship, because it's returning only product info, and it looks like:
public function product()
{
return $this->hasOne(ProductModel::class, 'id', 'product_id');
}
And I have defined sales in same model like so:
public function sales()
{
return $this->hasMany(OrderModel::class, ['product_id', 'product_id']);
}
I can see what is the problem, I'm not passing country id for sales relationships, but how can I pass the correct country_id? I have tried several options, but nothing seems to work as expected.
Any ideas on how can I solve it?
BTW I have installed \Awobaz\Compoships\Compoships
but I couldn't solve it either.
Upvotes: 1
Views: 91
Reputation: 2480
I cannot say I fully understand your question, but from what I understand you're not able to get sales from the UserCountries
Model
because there's no relational key on the sales table.
If this is the case, I highly suggest having a look at hasManyThrough
, which will retrieve a relation through another model.
Upvotes: 1