【发布时间】:2020-05-11 14:16:41
【问题描述】:
我想在主页视图刀片中显示总金额 (表 = 付款,冒号 = 金额)
我通过路由测试了另一种方法,它工作正常,但结果当然显示在 /test 中:
Route::get('/test', function(){
$total = DB::table('payments')
->where('status', '=', 'success') //this for collect only the success payments
->sum('payments.amount');
return $total; // work fine the result is correct
});
但我的目的是在主视图中显示此结果 通过将前面代码中的函数从路由移动到控制器并在视图中调用它
对于控制器,我有 Homecontroller,但功能索引已经使用,所以我在这个控制器中创建新功能,我试试这个
public function show(){
$total = DB::table('payments')
->where('status', '=', 'success')
->sum('payments.amount');
return view('home' , $total);
}
为了路由我把这个
Route::get('/home', 'HomeController@show');
我在视图中尝试了这个,但没有用:
<h1>{{$total}}</h1>
【问题讨论】:
标签: php laravel controller