2017-08-03 73 views
1

这是我的控制器功能为什么laravel查看返回数组?而不是价值?

public function index() 
{ 
    $currentdate = Carbon::yesterday(); 
    $totalsales = DB::table('receipts') 
     ->whereDate('created_at','=', $currentdate) 
     ->where('status','=', 'served') 
     ->orderBy('created_at','asc') 
     ->select(DB::raw('SUM(amount_due) as totalsales')) 
     ->get(); 
     // ->first(); 
     // return $totalsales; 
    return view('dashboard.index',compact('totalsales')); 
} 

这是我的视图

<div class="panel-body"> 
<h2>{{$totalsales}}</h2> 

insted的值本身的视图的返回这样

[{"totalsales":"130.00"}] 
+0

使用get(),您将得到收集 –

+0

@SagarGautam - >等什么我前人的精力呢? –

+0

如果你只需要记录,你可以使用' - > first()',否则使用' - > get()'。在上面的例子中,你可以在'$ totalsales'上使用foreach。 –

回答

1

我想你只需要在amount_due列的总和,所以你可以使用下面的查询。

$totalsales = DB::table('receipts') 
       ->whereDate('created_at','=', $currentdate) 
       ->where('status','=', 'served') 
       ->orderBy('created_at','asc') 
       ->sum('amount_due'); 

保持其他代码相同,这将为你工作。

希望你能理解

0

它没有按阵列不知道只有一个领域。

您需要可以指定

<h2>{{ $totalsales->totalsales }}</h2> 

或更改您的查询:

$totalsales = DB::table('receipts') 
    ->whereDate('created_at','=', $currentdate) 
    ->where('status','=', 'served') 
    ->orderBy('created_at','asc') 
    ->select(DB::raw('SUM(amount_due) as totalsales')) 
    ->first() 
    ->totalsales; 
+0

我试过这个,但它告诉我这个errorCall未定义的方法stdClass :: value() –

+0

检查我的编辑。使用查询生成器(DB)而不是 - > value('totalsales'),您需要 - > totalsales – Jed

相关问题