首页 > 解决方案 > 在同一控制器 Laravel 中将变量从一种方法传递到另一种方法

问题描述

我需要从 store 方法中传递 select 选项值来显示我需要传递$typeg值来显示方法

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

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

我得到未定义的变量:

或者

函数 app\Http\Controllers\appController::show() 的参数太少,通过了 1 个,预期正好有 2 个

标签: laravel

解决方案


在第一个函数上试试这个,你有一些variable女巫你想把它传递给另一个函数\方法

比您需要使用$this的名称和您想传递的其他方法的名称var也像这样。

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

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

推荐阅读