这是我的控制器功能
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>
在视图中返回一个像这样的数组
[{"totalsales":"130.00"}]
答案 0 :(得分:1)
我认为您只需要amount_due
列的总和,以便您可以使用以下查询。
$totalsales = DB::table('receipts')
->whereDate('created_at','=', $currentdate)
->where('status','=', 'served')
->orderBy('created_at','asc')
->sum('amount_due');
保持其他代码相同,这对您有用。
希望你理解
答案 1 :(得分: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;