Laravel阵列输出

时间:2016-04-28 15:25:31

标签: php laravel eloquent

我正在控制器中执行查询并在我的视图中输出总费用。

继承我的疑问:

 $stat = array(
            'TotalCost' => DB::table('ORD_DETAIL')
                        ->select(DB::raw('SUM(OD_QTYORD * OD_QTYUNIT * OD_COSTPRICE) as TotalCost'))
                        ->where('OD_ORDER_NUMBER',$id)->first()
            );

继承我的输出:

{{ $stat['TotalCost'] }}

但我收到错误:

  

htmlentities()期望参数1为字符串,给定对象(查看:   F:\ view.blade.php)

2 个答案:

答案 0 :(得分:2)

您收到上述错误的原因是您的查询返回Object

要解决这个问题,你可以这样做:

{{ $stat['TotalCost']->TotalCost }}

或者您可以更改查询以使用Laravel的内置sum()方法:

DB::table('ORD_DETAIL')
    ->where('OD_ORDER_NUMBER',$id)
    ->sum(DB::raw('OD_QTYORD * OD_QTYUNIT * OD_COSTPRICE'));

希望这有帮助!

答案 1 :(得分:0)

将其更改为

{{ $stat['TotalCost']->TotalCost }}

维持

{{ $stat['TotalCost'] }}

将您的查询更改为

 $stat = array(
        'TotalCost' => DB::table('ORD_DETAIL')
                    ->select(DB::raw('SUM(OD_QTYORD * OD_QTYUNIT * OD_COSTPRICE) as TotalCost'))
                    ->where('OD_ORDER_NUMBER',$id)->lists('TotalCost')->first();
        );