如何让多个关系正常工作?

时间:2013-12-19 19:58:03

标签: php mysql laravel-4 database-relations

目前,我可以从items表中提取所有产品,但由于在SizesToProducts表中设置了关系,我无法匹配sizes_id

数据库表如下:

  • 项目(id,product_id,数量)
  • 产品(id,名称)
  • 尺码(身份证,姓名)
  • SizesToProducts(id,product_id,sizes_id,price)

以下是我的模特:

//--- Item Class
class Item extends \Eloquent  {

    protected $table = '_cart_item';

    function product()
    {
        return $this->hasMany('App\Models\Product','id');
    }

} 

//--- Product Class
class Product extends \Eloquent  {

    protected $table = '_products';

    public function items()
    {
        return $this->belongsTo('Item');
    }

    public function size()
    {
        return $this->hasMany('Product','product_id');
    }
}

//--- Size Class
class Size extends \Eloquent  {

    protected $table = '_sizes';

    public function product()
    {
        return $this->belongsTo('SizeToProduct','sizes_id');
    }

}

//--- SizeToProduct Class
class SizeToProduct extends \Eloquent  {

    protected $table = '_sizes_to_products';

    public function items()
    {
        return $this->belongsTo('Product','sizes_id');
    }

}

1 个答案:

答案 0 :(得分:0)

您真的需要SizesToProducts表的id列吗?我对Laravel不太熟悉,但它有many-to-many关系。也许这就是你要找的东西?