Home > Software design >  The response receive after requesting next request
The response receive after requesting next request

Time:07-02

When requesting to update products, in database they update successfully but in the response of api for example the total price of products is 100 when I update them they have to be 150 so when I send a request to get 150 I find 100 and after I update again for let's say 200 I find 150 ... .
here's my update function

public function updateProducts(AdminUpdateOrderRequest $request, $id)
    {

        $orderValidated = $request->validated();

        $order = Order::findOrFail($id);

        if (isset($orderValidated['products'])) {
            $order->products()->sync($orderValidated['products']);
        }

        DB::table('order_product')->where('order_id', $order->id)
            ->update([
                'total_buy_price' => DB::raw('order_product.quantity * (select converted_buy_price from products where id = order_product.product_id)'),
                'total_selling_price' => DB::raw('order_product.quantity * (select converted_selling_price from products where id = order_product.product_id) ')
            ]);

        DB::table('orders')->where('id', $order->id)->update([
            'cost_price' => DB::raw('(select SUM(order_product.total_buy_price) from order_product where order_product.order_id = ' . $order->id . ')'),
            'total_price' => DB::raw('(select SUM(order_product.total_selling_price) from order_product where order_product.order_id = ' . $order->id . ')'),
            'remaining' => DB::raw('(select SUM(order_product.total_buy_price) from order_product where order_product.order_id = ' . $order->id . ' - ' . $order->payments->sum('amount') . ')'),
            'products_count' => DB::raw('(select SUM(quantity) from order_product where order_product.order_id = ' . $order->id . ')')
        ]);

        DB::table('packages')->update([
            'selling' => DB::raw('(select SUM(orders.total_price) from orders where orders.package_id = packages.id)'),
            'amount' => DB::raw('(select SUM(orders.total_price) from orders where orders.package_id = packages.id)'),
            'cost' => DB::raw('(select SUM(orders.cost_price) from orders where orders.package_id = packages.id)'),
        ]);

        return OrderResource::make($order)->additional([
            'success' => true,
        ]);
    }

CodePudding user response:

I solved my problem by updating orders in this way

    public function updateProducts(AdminUpdateOrderRequest $request, $id)
    {

        $orderValidated = $request->validated();

        $order = Order::findOrFail($id);

        if (isset($orderValidated['products'])) {
            $order->products()->sync($orderValidated['products']);
        }
        
        DB::table('order_product')->where('order_id', $order->id)
            ->update([
                'total_buy_price' => DB::raw('order_product.quantity * (select converted_buy_price from products where id = order_product.product_id)'),
                'total_selling_price' => DB::raw('order_product.quantity * (select converted_selling_price from products where id = order_product.product_id) ')
            ]);
            
        $order->cost_price = DB::table('order_product')->where('order_product.order_id', '=', $order->id)->sum('order_product.total_buy_price');
        $order->total_price = DB::table('order_product')->where('order_product.order_id', '=', $order->id)->sum('order_product.total_selling_price');
        $order->remaining = $order->total_price - $order->payments->sum('amount');
        $order->products_count = DB::table('order_product')->where('order_product.order_id', '=', $order->id)->sum('order_product.quantity');

        DB::table('packages')->update([
            'selling' => DB::raw('(select SUM(orders.total_price) from orders where orders.package_id = packages.id)'),
            'amount' => DB::raw('(select SUM(orders.total_price) from orders where orders.package_id = packages.id)'),
            'cost' => DB::raw('(select SUM(orders.cost_price) from orders where orders.package_id = packages.id)'),
        ]);

        return OrderResource::make($order)->additional([
            'success' => true,
        ]);
    }

CodePudding user response:

Solution: trying to get product query after updating query. like this $order = Order::findOrFail($id); after update.

  • Related