How do I get multiple relationships working properly?

94 Views Asked by At

Currently, I can pull out all of the products from the items table, but I cannot match the sizes_id up as the relationship between is set in SizesToProducts table.

Database tables are as follows:

  • Items (id, product_id,quantity)
  • Products (id,name)
  • Sizes (id,name)
  • SizesToProducts (id, product_id, sizes_id, price)

Here are my models:

//--- 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

There are 1 best solutions below

1
On

Do you really need an id column for SizesToProducts table? I'm not quite familiar with Laravel, but It has many-to-many relation. Maybe this is what you are looking for?