Reputation: 87
I've created this online shopping cart, if a buyer buys with one checkout item, works well, but there is a problem when the buyer is shopping with multiple items, not working properly.
I need help, so that the items from the shopping cart can be put all into the database. My problem is inserting multiple products from the shopping cart into the database as individual rows. Can anyone help?
I want this because the items that have been purchased can be reviewed by the buyer who bought the item. so i can get "product_id".
Table: orders
Columns:
id int(10) UN AI PK
user_id int(11)
product_id int(11)
item_name text
payment varchar(255)
courier varchar(255)
note text
quantity int(11)
total int(11)
status int(11)
OrderController.php
public function NewOrder(Request $request)
{
$this->validate($request, [
'payment' => 'required',
'courier' => 'required',
]);
$cart = Session::get('cart');
$total = 0;
foreach ($cart as $data) {
$total_harga = $data['harga'] * $data['qty'];
$qty = $data['qty'];
}
$quantity = $qty + 0;
$new = new Orders();
$new->user_id = Auth::user()->id;
$new->product_id = $data['id'];
$new->item_name = $data['item_name'];
$new->payment = $request['payment'];
$new->courier = $request['courier'];
$new->note = $request['note'];
$new->quantity = $quantity;
$new->total = $total_harga;
$new->status = 1;
$new->save();
$id = $new->id;
Session::forget('cart');
return redirect()->route('order.status', $id);
}
how to inserting multiple products from the shopping cart into the database as individual rows?
Thanks for the answer :)
Upvotes: 3
Views: 6326
Reputation: 2806
To add multiple ordered items in database, you have to add 2 tables; orders and ordersproducts.
For adding all your ordered items
after placing order, create one more table like ordersproducts
where you will add your item details like product id, product name, description, unit price, quantity and the orders
table must have main detail of the orders only like order number, user id, total products, total amount, order date. So orders table
and ordersproducts table
will look like below :
Table: orders
id int(10) UN AI PK
user_id int(11)
payment varchar(255)
courier varchar(255)
note text
total_quantity int(11)
total_amount int(11)
status int(11)
Table: ordersproducts
id int(10) UN AI PK
order_id int(11)
product_id int(11)
item_name text
quantity int(11)
price int(11)
OrderController.php
public function NewOrder(Request $request)
{
$this->validate($request, [
'payment' => 'required',
'courier' => 'required',
]);
$cart = Session::get('cart');
$total = 0;
foreach ($cart as $data) {
$total_harga = $data['harga'] * $data['qty'];
$qty = $data['qty'];
}
$quantity = $qty + 0;
$new = new Orders();
$new->user_id = Auth::user()->id;
$new->payment = $request['payment'];
$new->courier = $request['courier'];
$new->note = $request['note'];
$new->total_quantity = $quantity;
$new->total_amount = $total_harga;
$new->status = 1;
$new->save();
$order_id = DB::getPdo()->lastInsertId();
foreach ($cart as $data) {
$total_harga = $data['harga'] * $data['qty'];
$qty = $data['qty'];
$OrderPro = new Ordersproducts;
$OrderPro->order_id = $order_id;
$OrderPro->product_id = $data['product_id'];
$OrderPro->product_name = $data['product_name'];
$OrderPro->product_price = $data['product_price'];
$OrderPro->product_quantity = $data['product_quantity'];
$OrderPro->save();
}
Session::forget('cart');
return redirect()->route('order.status', $id);
}
Please make changes in the above code as per your requirement. I have just given you a hint so that you can make 2 tables one for storing main details of order and one for complete product ordered details that you want.
Upvotes: 4