'How to use a previous raw select calculation for next select raw calculation in Laravel?

query() ->select([
            'order_items.id',
            DB::raw("(order_qty * mp_price) as grossamount"),
            'order_items.discount_amount',
            DB::raw("(grossamount - discount_amount) as netamount"),
        ])

I am using Laravel Spatie Query Builder, but it should work as eloquent. How so I can use grossamount in my next raw statement, as seen above. The above does not work. I want to use the grossamount for netamount. Thank you!



Solution 1:[1]

try to use :

query() ->select([
            'order_items.id',
            DB::raw("(order_qty * mp_price) as grossamount"),
            'order_items.discount_amount',
            DB::raw("((order_qty * mp_price) - discount_amount) as netamount"),
        ])

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 gouki