Hello, I am a programmer coming from the world of node.js/javascript and just did my first bigger laravel project after a few smaller ones.
I made a classic ecommerce project, the only thing I struggled with or found easier with javascript, was the setup of my database structure (Orders, Cart, etc.)
For the cart of my users, I didn't actually make a Cart table, but a many-to-many relationship with pivot table between users and products.
For the orders, I made an Order table and OrderItem table, where the orderitems have an order_id reference to the order.
It all works, but honestly it doesn't look great even to me, so I have few questions:
- Obvious one, anyone have a better way of setting this up?
2)I made a many-to-many relationship between users and products, but what would I need to if I wanted 2 many-to-many relationships? For example a user can have a cart, but also a list of favorite items.
3)This is my logic for creating an order:
public function store() {
$order = Order::create([
'user_id' => request()->user()->id
]);
$totalAmount = 0;
if(count(request()->user()->products) == 0) {
return ['msg' => 'No products in cart.'];
} else {
foreach(request()->user()->products as $p) {
global $totalAmount;
$product = Product::find($p->pivot->product_id);
$amount = $p->pivot->quantity * $product->price;
$totalAmount += $amount;
OrderItem::create([
'user_id' => request()->user()->id,
'product_id' => $p->pivot->product_id,
'order_id' => $order->id,
'quantity' => $p->pivot->quantity,
'price' => $product->price,
'amount' => $amount
]);
request()->user()->products()->detach($p->id);
}
$order->update(['amount' => $totalAmount]);
return ['msg' => $order];
}
}
- I create an order with a default amount (= money to pay) of 0.
- I go over the products in my cart if there are any, and look for the product in the database, because I figured I wanted to the price to be the current price.
- I add the amount to pay for each item to $totalAmount variable
- I create an orderitem with an order_id.
- I remove the items from my cart
- I update the order with an actual amount (= $totalAmount)
This works, but I don't like it one bit and I can't imagine there isn't a better approach to this, so I would be happy to hear it. This can either be a better way of writing current code (I'm relatively new to php and laravel), or a totally different setup alltogether.
Thanks in advance.