一个型号上有两个不同的外键

时间:2017-05-14 12:46:36

标签: php laravel model eloquent

我有这3张桌子

产品

  • id
  • product_type_id
  • 名称

product_attributes

  • ID
  • product_type_id
  • 名称

product_attribute_values

  • ID
  • attribute_id
  • PRODUCT_ID
  1. 产品有多个属性,它们与 product_type
  2. 相关联
  3. 属性有多个属性值,它们与 attribute_id
  4. 相关联
  5. 属性值与一个属性和一个产品相关,它们与 product_id 的产品相关
  6. 我创建了3个模型,Product,ProductAttribute和ProductAttributeValue

    产品型号

    public function attributes(){  
        return $this->hasMany('App\ProductAttribute', 'product_type_id',   'product_type_id');
    }
    

    产品属性模型

    public function attributeValues(){
        return $this->hasMany('App\ProductAttributeValue', 'attribute_id', 'id');
    }
    

    然后当我试图获得具有所有相关属性和属性值的产品时

    $product = Product::find(1)->with('attributes.attributeValues')->get()->toArray();
    

    正在提取该属性的所有属性值。

    问题是我不确定如何为两个不同的型号提供2个外键......

    我希望我解释得很好。

1 个答案:

答案 0 :(得分:0)

>>> from statistics import mode
>>> mode('Hello I like pie because they are like so good'.split())
'like'