Subtract column value from sum() of another column
$month_revenue = Sale::whereMonth('payment_date', $month_label)->whereYear('payment_date', $year_label)->sum('total');
I need to subtract shipping_cost column from this query, would it be possible with eloquent?
Think this is the closest you'll get:
$month_revenue = Sale::selectRaw('SUM(total - shipping_cost) as month_revenue')
->whereMonth('payment_date', $month_label)
->whereYear('payment_date', $year_label)
->first()
->month_revenue;
Thanks @frankincredible
I ended doing it in 2 queries but I'll give your method a try anyway.
@klm113 - give it a shot. This solution will definitely be more performant than two separate database queries.
@klm113 did my solution help? If so, please don't forget to mark it as Best Answer. :)
Please or to participate in this conversation.