Pass variable from one method to another in same controller Laravel

馋奶兔 提交于 2021-01-28 07:05:31

问题


I need to pass select option value from store method to show i need to pass the $typeg value to show method

public function store(Request $request ) {
   $typeg = $request->input('type');
}

public function show($id) {
   dd($this->store($typeg));
}

i get Undefined variable:

or

Too few arguments to function app\Http\Controllers\appController::show(), 1 passed and exactly 2 expected


回答1:


Store your data on session (or somewhere else like cookie, cache, database). So you can reach the data later.

class SomeController extends Controller {


public function store(Request $request ) {
   session(["typeg"=>$request->input('type')])
}

public function show($id) {
   dd(session("typeg"));
}



回答2:


Try this on the first function you have some variable witch you want to pass it to another function\method

Than you need to use $this and the name of the other method you'd like to pass the var too something like this.

public function oneFunction(){
    $variable = "this is pretty basic stuff in any language";
    $this->anotherFunction($variable)
}

public function anotherFunction($variable){
    dd($variable);
}


来源:https://stackoverflow.com/questions/51855119/pass-variable-from-one-method-to-another-in-same-controller-laravel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!