我想在主頁視圖邊欄選項卡中顯示總金額(表 = 付款,冒號 = 金額)我通過路由測試了另一種方法,這工作正常,但結果顯示在/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,但功能索引是aleardy使用的,所以我在這個控制器中創建新函數,我試試這個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>
3 回答

莫回無
TA貢獻1865條經驗 獲得超7個贊
您可以使用緊湊的函數發送總變量結果,如下所示。
return view('home' , compact('total'));
// You can call it in home.blade like this
//for example
@if(isset($total ))
<li>{{ $total }}</li>
@endif

烙印99
TA貢獻1829條經驗 獲得超13個贊
您可以使用with()
public function show(){
$total = DB::table('payments')->where('status', '=', 'success')
->sum('payments.amount');
return view('home')->with('total', $total);
}
- 3 回答
- 0 關注
- 145 瀏覽
添加回答
舉報
0/150
提交
取消