I develop an e-commerce application with Laravel 4 and need an advice to solve my database design problem.
The Problem:
My client has products which have multiple variations/options. For example:
Product1 = Apple Iphone 5
Color Option : Black
Option1 For Black: 16GB
Stock for Option1 : 15
Sales Price for Option1 : $900
Option2 For Black: 32GB
Stock for Option2 : 32
Sales Price for Option2 : $1.200
Option3 For Black: 128GB
Stock for Option3 : 24
Sales Price for Option3 : $1.700
Color Option : White
Option1 For White: 32GB
Stock for Option1 : 9
Sales Price for Option1 : $930
Option2 For White: 64GB
Stock for Option2 : 12
Sales Price for Option2 : $1.270
Product2 = Motorola Xoom 9868AB Case
Material Option : Leather
Option1 For Leather: Grey
Stock for Option1 : 90
Sales Price for Option1 : $12
Option2 For Leather: Pink
Stock for Option2 : 12
Sales Price for Option2 : $12.70
Material Option : Hard Shell
Option1 For Hard Shell: Black
Stock for Option1 : 51
Sales Price for Option1 : $32.90
My approach:
As you see above, there is many products with different type of options,stocks,prices. I've tried something like the following with belongsToMany():
Table: products
id
name
created_at
updated_at
Table: variations
id
title
parent
created_at
updated_at
Table: product_variation
id
products_id
variations_id
stock
purchase_price
sales_price
created_at
updated_at
The Products model:
<?php
class Products extends Eloquent {
protected $table = 'products';
protected $fillable = array('name');
public function variations() {
return $this->belongsToMany('Variations','product_variation')
->withPivot('purchase_price', 'stock', 'sales_price')
->withTimestamps();
}
}
How should be the Orders Model? The orders would hasMany() products and Variations. How would be your approach to the problem?