2017-05-17 71 views
0

GetEstimateController.php如何laravel調用一個函數在同一個控制器中的另一個函數內部5.2

class GetEstimationController extends Controller 
{ 
    public function fill_dropbox(){ 
     $data = ProcedureTreat::get(['pro_name']); 
     $data1 = HospitalPackage::get(['address']); 
     // return $this->get_estimate(); 
     // dd($data); 
     return View::make('get_quote')->with('address',$data1)->with('procedureName',$data); 
    } 
    public function get_estimate($name,$address){ 
     $data = HospitalPackage::where(['pro' => 'name', 'address' => 'address'])->get(); 
     $Dropbox_fill = $this->fill_dropbox(); 
     // dd($Dropbox_fill); 
     return View::make('get_quote')->with('HospitalPack',$data); 
    } 
} 

routes.php文件

Route::get('/',[ 
    'uses' => '[email protected]_dropbox', 
    'as' => 'fill.dropbox' 
]); 
Route::any('/find/{name}/{address}',[ 
    'uses' => '[email protected]_estimate', 
    'as' => 'get.estimate' 
]); 

get_quote.blade.php

@if(isset($procedureName)) 
     @foreach($procedureName as $key => $data) 
     <option value="{{$data->pro_name}}">{{$data->pro_name}}</option> 
     @endforeach        
    @endif 
    @if(isset($address)) 
     @foreach($address as $key => $add) 
     <option value="{{$add->address}}">{{$add->address}}</option> 
     @endforeach 
    @endif 

我想在get_estimate函數裏面調用fill_dropbox函數並想發送數據到再次。

使用$Dropbox_fill = $this->fill_dropbox();我得到的數據

View {#155 ▼ 
    #factory: Factory {#141 ▶} 
    #engine: CompilerEngine {#148 ▶} 
    #view: "get_quote" 
    #data: array:2 [▼ 
    "address" => Collection {#2507 ▼ 
     #items: array:1382 [▼ 
     0 => HospitalPackage {#2508 …24} 
     1 => HospitalPackage {#2509 …24} 
     2 => HospitalPackage {#2510 …24} 
     3 => HospitalPackage {#2511 …24} 
     } 
    "procedureName" => Collection {#1124 ▶} 
    ] 
    #path: "C:\wamp\www\IIMTC\bmmt_new\resources\views/get_quote.blade.php" 
} 

所以我怎麼能與以前foreach訪問它get_quote.blade.php

+0

返回查看視圖訪問::讓( 'get_quote') - > with('data',['HospitalPack'=> $ data,'Dropbox_fill'=> $ Dropbox_fill]); – JYoThI

+0

試試我的答案@Giridhari Lal – JYoThI

回答

0

有多種方式可以將數據發送到視圖。其中之一是如下,

return View::make('get_quote', ['HospitalPack' => $data,'Dropbox_fill' => $Dropbox_fill]); 

和你的視圖文件名應該是get_quote.blade.php

+0

如何在'get_quote'頁面中獲取'Dropbox_fill'數組元素? –

+0

@Kuru已經在你的回答中發佈了你如何在你的視圖中訪問這個變量。您可以使用相同的代碼 – manian

+0

使用此代碼顯示同樣的錯誤。 '提供給foreach()的無效參數(view:C:\ wamp \ www \ IIMTC \ bmmt_new \ resources \ views \ get_quote.blade.p hp)'@manian –

0

嘗試這樣

public function get_estimate($name,$address){ 
     $HospitalPack= HospitalPackage::where(['pro' => 'name', 'address' => 'address'])->get(); 
     $Dropbox_fill = $this->fill_dropbox(); 
     // dd($Dropbox_fill); 
     return View::make('get_quote',compact('HospitalPack','Dropbox_fill')); 
    } 

然後在視圖中,可以像這樣訪問

@if(isset($HospitalPack)) 
     @foreach($HospitalPackas as $key => $data) 
     #your data 
     @endforeach        
    @endif 
@if(isset($Dropbox_fill)) 
      @foreach($Dropbox_fill as $key => $data) 
      #your data 
      @endforeach        
     @endif 
+0

控制器中'Dropbox_fill'旁邊有一個空格碼。你可以請更正這一個 – manian

+0

@manian修正:) – Kuru

+0

'爲foreach()提供了無效的參數()(查看:C:\ wamp \ www \ IIMTC \ bmmt_new \ resources \ views \ get_quote.blade.php)此錯誤顯示。 –

0

傳遞這樣

0數據

在這樣

$data['HospitalPack']; 
    $data['Dropbox_fill']; 

或使用COMPACT()

return View::make('get_quote',compact('HospitalPack','Dropbox_fill'); 

視圖訪問數據。在這樣的

$HospitalPack 
相關問題