Apr 3, 2023
0
Level 2
Many Through or something else?
Hi! I'm really confused on how to achieve the following. I have 4 tables
- Products (with Product model)
- Product prices ( with ProductPrice model)
- Contracts (with Contract model)
- Contracts Product Prices (since it's the pivot table, no model)
a Product can have multiple prices
public function prices()
{
return $this->hasMany(ProductPrice::class);
}
A price belongs to a product
public function product()
{
return $this->belongsTo(Product::class);
}
A contract has many product prices
public function productPrices()
{
return $this->belongsToMany(ProductPrice::class);
}
but how do I get the products from the contracts doing $contract->products ?
I tried
public function products()
{
return $this->hasManyThrough(Product::class, ProductPrice::class);
}
but it's not working, telling me Column not found: 1054 Unknown column 'product_prices.contract_id' in 'field list'
I'm not quite sure how to formulate it properly.
Thanks!
Please or to participate in this conversation.