I make order management system, how to total The price in laravel?

Viewed 396

I make order management system in laravel. This is my table structure is When user gives order The controller will fill 2 tables 1- orders , 2- orderitems in orders table there are 2 fields user_id and total (price). and in orderitems controller will create different columns for product example if user place 2 product than controller will create 2 row in orderitems table there are 5 fields order_id , product_id , quantity , price , total (for particular product)

This is my orders table enter image description here

this is my orderitems table

enter image description here

the doubt is that how i can add price of different columns of orderitems table and show it in total of orders table this is my OrderController

public function store(Request $request)
{
    $order = Order::create([
        'user_id' => $request->input('user_id'),
        'total' => **Doubt here**,
    ]);
    $size = count(collect($request)->get('quantity'));
    for ($i = 0; $i < $size; $i++) {
        $orderitem = Orderitem::create([
            'order_id' => $order->id,
            'product_id' => $request->get('product_id')[$i],
            $quantity = $request->get('quantity')[$i],
            'quantity' => $quantity,
            $price = Product::find($request->get('product_id')[$i])->price,
            'price' => $price,
            'total' => $quantity * $price,
        ]);
    }

    return redirect()->route('orders.index');
}
2 Answers

In your situation.I think this will be ok

public function store(Request $request)
{
    $order = Order::create([
        'user_id' => $request->input('user_id'),
        'total' => 0,
    ]);
    $total = 0;
    $size = count(collect($request)->get('quantity'));
    for ($i = 0; $i < $size; $i++) {
        $orderitem = Orderitem::create([
            'order_id' => $order->id,
            'product_id' => $request->get('product_id')[$i],
            $quantity = $request->get('quantity')[$i],
            'quantity' => $quantity,
            $price = Product::find($request->get('product_id')[$i])->price,
            'price' => $price,
            'total' => $quantity * $price,
        ]);
        $total += $orderitem['quantity']*$orderitem['price'];
    }
    $order['total'] = $total;
    $order->save()

    return redirect()->route('orders.index');
}

but in some more reality scenario.we will do order calculation more complex such as discocunt.so we will know order total before saved to database.
if you have more question.please comment to me.

If you are using relationship then you can do the following

 $order = Order::create([
        'user_id' => 1,

    ]);

    $order->product()->sync([
        1=>['quantity'=>2, 'price'=>20,'total'=>40],
        2=>['quantity'=>1, 'price'=>10,'total'=>10],
    ]);

    $order->total=$order->product()->sum('total');
    $order->save();

As i posted relationship in other post which relating to this then you can do the above method. Ref:Attempt to read property "price" on null in laravel?

Related