如何在laravel 5.2中的同一控制器中的另一个函数内调用一个函数 [英] How to call one function inside another function in same controller in laravel 5.2

查看:126
本文介绍了如何在laravel 5.2中的同一控制器中的另一个函数内调用一个函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GetEstimateController.php

  class GetEstimationController extends Controller 
{
public function fill_dropbox(){
$ data = ProcedureTreat :: get(['pro_name']);
$ data1 = HospitalPackage :: get(['address']);
//返回$ this-> get_estimate();
// dd($ data);
返回视图:: make('get_quote') - &(使用('procedureName',$ data)'('address',$ data1) - >;
}
public function get_estimate($ name,$ address){
$ data = HospitalPackage :: where(['pro'=>'name','address'=>'地址]) - >得到();
$ Dropbox_fill = $ this-> fill_dropbox();
// dd($ Dropbox_fill);
返回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



<$ p $
@foreach($ procedureName as $ key => $ data)
< option value ={{$ data- > pro_name}}> {{$ DATA-> pro_name}}< /选项>
@endforeach
@endif
@if(isset($ address))
@foreach($ address为$ key => $ add)
< option值= {{$添加 - >地址}} > {{$添加 - >地址}}< /选项>
@endforeach
@endif

我想调用 fill_dropbox funtion在 get_estimate 函数中,并希望将数据发送到 get_quote.blade.php

使用 $ Dropbox_fill = $ this-> fill_dropbox(); p>

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

所以我如何在 get_quote.blade.php 与之前的 foreach

解决方案

像这样

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

查看访问这样的数据

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

或使用COMPACT()



<$ p $ ('HospitalPack','Dropbox_fill');

查看这样的访问

  $ HospitalPack 


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

I want to call fill_dropbox funtion inside get_estimate function and want to send data to get_quote.blade.php again.

Using $Dropbox_fill = $this->fill_dropbox(); I'm getting data

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"
}

so how can I access it in get_quote.blade.php with previous foreach .

解决方案

Pass the data like this

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

In view access data like this

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

OR USE COMPACT()

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

In view access like this

   $HospitalPack

这篇关于如何在laravel 5.2中的同一控制器中的另一个函数内调用一个函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆