亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

功能顯示結果是控制器內部的視圖刀片(LARAVEL,PHP)

功能顯示結果是控制器內部的視圖刀片(LARAVEL,PHP)

PHP
拉風的咖菲貓 2022-08-19 09:59:02
我想在主頁視圖邊欄選項卡中顯示總金額(表 = 付款,冒號 = 金額)我通過路由測試了另一種方法,這工作正常,但結果顯示在/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


查看完整回答
反對 回復 2022-08-19
?
烙印99

TA貢獻1829條經驗 獲得超13個贊

您可以使用with()


public function show(){

    $total = DB::table('payments')->where('status', '=', 'success')

                                  ->sum('payments.amount'); 

    return  view('home')->with('total', $total); 

}


查看完整回答
反對 回復 2022-08-19
?
森林海

TA貢獻2011條經驗 獲得超2個贊

根據文檔:

第二個參數是應提供給視圖的數據的“數組”。

嘗試:

return  view('home' , ['total' => $total]);

或使用 :->with('key',$value)

return view('home')->with('total', $total);

你也可以使用compact()函數,它是PHP中的內置函數,用于使用變量創建數組:

return  view('home' , compact("total"));


查看完整回答
反對 回復 2022-08-19
  • 3 回答
  • 0 關注
  • 145 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號