2017-08-03 55 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

相關問題