Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Inventory Database Design

Sunday, June 25, 2023 1:29 PM

Tables:
Objectives for database design for Tshirt Design company 1) Customer table

2) product purchase table

3) products table
id Customer name email id Custom Product attribut Variation stock
4) Attribute table er id id e id id
1 A A@yahoo.com 1 1 3 2 4 15
5) Variation table 2 1 3 2 5 4
2 B B@gmail.com
3 1 5 6 13 13
id Customer id Product id attribute id Variation Stock Q) Print the invoice for each customer A and B 4 2 4 5 14 25
id
Output
1 1 3 2 4 15
2 1 3 2 5 4
Customer name product type quantity
3 1 5 5 13 13
a Oil Palm oil 30 ml
4 2 4 5 14 25 Olive oil 60 ml Based on the information provided, it
seems that the relationship between the
id Product name "product purchase" table and the
water soft 60 ml
3 Oil hard 80 ml "products" table can be inferred as follows:
4 Water
1. The "product purchase" table has a
5 Fruit "belongsTo" relationship with the
"products" table.
Answer:
- The "product purchase" table has a
column named "Product id", which likely
customer can purchase many products
represents a foreign key referencing the
"id" column in the "products" table.
A customer hasMany product Purchase
- Each row in the "product purchase"
table corresponds to a specific product
purchase, and it is associated with a single
product from the "products" table.
id Customer id Product id attribute id Variation Stock
id 2. The "products" table has a "hasMany"
id attribute name Product id Total Stock 1 1 3 2 4 15 relationship with the "product purchase"
table.
1 Olive oil 3 28 2 1 3 2 5 4
- The "products" table has an "id"
2 Palm oil 3 116 3 1 5 6 13 13 column, which serves as the primary key
3 Hard Water 4 940 for the products.
- Each product in the "products" table
4 Green 5 254 may have multiple associated product
5 Soft Water 4 170 purchases in the "product purchase" table.

Therefore, based on the information


id attribute id variation Unit stock price provided, the relationship between the
1 1 30 ml 14 140 two tables can be defined as:
2 1 60 ml 14 270 - The "product purchase" table belongs to
3 2 1 l 32 300 the "products" table (belongsTo).
4 2 500 ml 25 244 - The "products" table has many "product
purchases" (hasMany).
5 2 5 l 16 145
6 2 10 l 56 234 Please note that this is an inferred
relationship based on the column names
7 3 500 ml 140 245
and the assumption that the "Product id"
8 3 1 l 200 100 column in the "product purchase" table
9 3 5 l 600 243 references the "id" column in the
"products" table. The actual relationships
10 4 15 kg 50 255
should be defined in the model classes to
11 4 20 kg 70 987 establish the proper associations in your
12 4 60 gm 50 245 application.

13 6 20 kg 180 12
14 5 20 ml 254 15

Solution

$customer_product_purchases = ProductPurchase::with('customer')
->with('product')
->with('attribute')
->with('variation')
->get();
echo '<table>';
echo '<thead>';
echo '<tr>';
echo '<th>Product Purchase ID</th>';
echo '<th>Customer Name</th>';
echo '<th>Customer Email</th>';
echo '<th>Product Name</th>';
echo '<th>Attribute Name</th>';
echo '<th>Variation</th>';
echo '</tr>';
echo '</thead>';
echo '<tbody>';

// dd($customer_product_purchases);
$customer_ids = [];

Inventory Database Design Page 1


$customer_ids = [];

// dd($customer_product_purchases);
foreach($customer_product_purchases as $key => $product){
// dd($product->attribute->attribute_name);
if(in_array($product->customer->id,$customer_ids,)){
// dd($product->customer->Customer_name);
echo '<tr></tr>';
echo '<td rowspan="1">'.'</td>';
echo '<td rowspan="1">'. '</td>';
echo '<td>' . $product->customer->email . '</td>';
echo '<td>' . $product->product->Product_name . '</td>';
echo '<td>' . $product->attribute->attribute_name . '</td>';
echo '<td>' . $product->variation->variation . '</td>';
echo '<td>' . $product->stock . '</td>';
echo '</tr>';
}
else{
array_push($customer_ids,$product->customer_id);
echo '<tr>';
echo '<td>'. count($customer_ids) .'</td>';
echo '<td>' . $product->customer->Customer_name . '</td>';
echo '<td>' . $product->customer->email . '</td>';
echo '<td>' . $product->product->Product_name . '</td>';
echo '<td>' . $product->attribute->attribute_name . '</td>';
echo '<td>' . $product->variation->variation . '</td>';
echo '<td>' . $product->stock . '</td>';
echo '</tr>';

}
}

});

Output:

Inventory Database Design Page 2

You might also like