使用laravel查询查询来自同一表的多个列的查询

时间:2017-07-19 11:22:14

标签: laravel

这似乎很容易查询,但无法将其翻译成laravel查询。我有表orders有两列total_usdtotal_gbp。我想总结每一列,以获得页面上的usd总数和gbp总数。

此查询在phpmyadmin中运行,我得到了正确的结果

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

在laravel中,我试过这个

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

当我dd($sumOrders)我有一个非常巨大的输出,几乎导致浏览器冻结。 我的错误在哪里?

2 个答案:

答案 0 :(得分:1)

您可以尝试这样的事情

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

答案 1 :(得分:0)

您可以使用selectRaw

{
  "production_deploys": {
    "2017-07-08": {
      "failures": 2,
      "success": 1
    },
    "2017-07-09": {
      "failures": 2,
      "success": 1
    },
    "2017-07-10": {
      "failures": 2,
      "success": 3
    },
    "2017-07-11": {
      "failures": 2,
      "success": 10
    },
    "2017-07-12": {
      "failures": 2,
      "success": 2
    },
    "2017-07-13": {
      "failures": 2,
      "success": 2
    },
    "2017-07-14": {
      "failures": 2,
      "success": 1
    },
    "2017-07-15": {
      "failures": 2,
      "success": 1
    },
    "2017-07-16": {
      "failures": 2,
      "success": 1
    },
    "2017-07-17": {
      "failures": 2,
      "success": 3
    },
    "2017-07-18": {
      "failures": 2,
      "success": 10
    }
  },
  "pull_requests": {

  },
  "test_deploys": {

  },
  "uptime": {

  }
}