Each store ships products through shipping_methods. For example store1 ships with FedEx and UPS. But each store can restrict its shipping methods per product.
For example product1 ships only via FedEx by store1 while product2 can get shipped via both FedEx and UPS.
My table structure is as follows:
stores
- id
delivery_methods
- id
products
- id
I have a pivot table which connects stores with delivery_methods implementing a ManyToMany relationship:
store_delivery_method
- id
- store_id
- delivery_method_id
I need a way to connect this pivot to multiple products so that every product in every store may have multiple delivery/shipping methods. My idea is to create another table like so:
delivery_method_product_store
- store_delivery_method_id
- product_id
The catch is, product1 of store1 cannot have a delivery_method of, for example DHL since the store manager doesn’t work with that company.
Is this the correct way? If so, how should I write the model relationships?
Thanks in advance.
PS: My models so far:
class Store extends Model
{
public function deliveryMethods()
{
return $this->belongsToMany(DeliveryMethod::class,'store_delivery_method');
}
}
class DeliveryMethod extends Model
{
public function stores()
{
return $this->belongsToMany(Store::class, 'store_delivery_method');
}
}