Reputation: 907
In controller
class acontroller extends Controller
{
private $variable;
public function __construct(){
$this->variable;
}
public function first(){
$calculation = 1 + 1;
$this->variable = $calculation;
return view('something.view');
}
public function second(){
dd($this->variable);
return view('something.view2');
}
}
This is an example. What I'm trying to do is to pass the calculation result in first method to the second. I am expecting in second method inside dd() to show the result 2 but instead of this I am getting null.
What is going wrong and how to fix this?
Upvotes: 5
Views: 5558
Reputation: 324
you are calling first method in one http call and the second in another one so these two are completely separate run of your application if you want to share a variable in different requests you should use some kind of database
Upvotes: 0
Reputation: 21
Why don't you use some session variable?
Session::put('calculation', $this->variable);
and
$value = Session::get('calculation');
Upvotes: 1
Reputation: 163748
You really should redesign it. What you could do is create third method and do calculations in it. Then just call this method from first and second ones.
public function first(){
$this->third();
dd($this->variable);
return view('something.view');
}
public function second(){
$this->third();
dd($this->variable);
return view('something.view2');
}
public function third(){
$calculation = 1 + 1;
$this->variable = $calculation;
}
Just insert $this->second();
right after $this->variable = $calculation;
in the first
method.
Upvotes: 3