1
votes

I am trying to develop multiple-shop eCommerce platform. So the following tables created:

shops: id, name, ...
products: id, shop_id, ...
orders: id, ...
order_details: id, order_id, product_id, ...

And this is the summery of my models' relationships:

  • Shop->products: Shop hasMany Product
  • Product->shop: Product belongsTo Shop
  • Order->details: Order hasMany OrderDetail
  • OrderDetail->order: OrderDetail belongsTo Order
  • OrderDetail->product: OrderDetail belongsTo Product
  • Product->orders: Product hasMany OrderDetail

Now, 1: Is it possible to define Shop->orders relationship?(It seams has-many-through not works in this case due to multiple intermediate tables)

2: If it is possible, could the shop->orders->details contains only the records related to shop products?

It should be note that each Order may contain products from multiple shops but the shop->orders->details relation should contains only the shop products.

1

1 Answers

0
votes

There is no native relationship for this case.

I created a HasManyThrough relationship with support for BelongsToMany: Repository on GitHub

After the installation, you can use it like this:

class Shop extends Model {
    use \Staudenmeir\EloquentHasManyDeep\HasRelationships;

    public function orders() {
        return $this->hasManyDeep(Order::class, [Product::class, 'order_details']);
    }
}