问题
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