如何在laravel 5.2中的同一个控制器中调用另一个函数内的一个函数

时间:2017-05-17 12:00:56

标签: php arrays function laravel-5.2

GetEstimateController.php

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' => 'GetEstimationController@fill_dropbox',
    'as' => 'fill.dropbox'
]);
Route::any('/find/{name}/{address}',[
    'uses' => 'GetEstimationController@get_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

我想在fill_dropbox功能中调用get_estimate功能,并希望再次向get_quote.blade.php发送数据。

使用$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"
}

所以我如何使用之前的get_quote.blade.phpforeach中访问它。

3 个答案:

答案 0 :(得分:0)

有多种方法可以将数据发送到视图。其中一个如下,

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

,您的视图文件名应为get_quote.blade.php

答案 1 :(得分: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

答案 2 :(得分:0)

传递这样的数据

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

在视图中访问此类数据

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

或使用COMPACT()

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

在视图中访问

   $HospitalPack