I have these 3 tables
products
- id
- product_type_id
- name
product_attributes
- id
- product_type_id
- name
product_attribute_values
- id
- attribute_id
- product_id
- value
- Products have multiple attributes, they are related by product_type
- Attributes have multiple attribute values, they are related by attribute_id
- Attribute values are related to one attribute and one product, they are related to the product by product_id
I have created 3 models, Product, ProductAttribute and ProductAttributeValue
Product Model
public function attributes(){
return $this->hasMany('App\ProductAttribute', 'product_type_id', 'product_type_id');
}
Product Attribute Model
public function attributeValues(){
return $this->hasMany('App\ProductAttributeValue', 'attribute_id', 'id');
}
And then when im trying to get a product with all related attributes and attribute values
$product = Product::find(1)->with('attributes.attributeValues')->get()->toArray();
All of the attribute values for that attribute are being pulled in.
The problem is im not sure how to have 2 foreign keys to two different models...
I hope I explained it well enough.