1
votes

This seems very easy query but can't translate it into laravel query. I have table orders there are two columns total_usd and total_gbp. I want to sum each column to get total of usd and total of gbp on the page.

This query is working in phpmyadmin and I got correct result

SELECT sum(order_total_usd) as usd, sum(order_total_gbp) as gbp FROM `orders`

In laravel I've tried this

$sumOrders = Order::select('sum(order_total_gbp) as gbp, sum(order_total_usd) as usd');

when I dd($sumOrders) I've one really huge output which almost cause browser to freeze. Where is my mistake here?

2

2 Answers

1
votes

You can try something like this

$sumOrders = Order::select( \DB::raw("sum(order_total_gbp) as gbp"),  \DB::raw("sum(order_total_usd) as usd"))->get();
0
votes

You can use selectRaw

$sumOrders = Order::selectRaw('sum(order_total_gbp) as gbp, sum(order_total_usd) as usd');