Can you help me with my question if there is a way to access the variable that is in another method in a Laravel controller, please.
public function calculo1(){
$variable_a_la_que_quiero_acceder = 5;
return view('vista1', compact('variable_a_la_que_quiero_acceder'));
}
public function calculo2(){
$x = 2;
$resultado = $variable_a_la_que_quiero_acceder + $x;
return view('vista2', compact('resultado'));
}
I would do it this way:
I hope it works for you.
In Laravel nobody prevents you from having class variables and methods that don't return a view, that is, you could do something like this:
This way you can access the variable in as many methods as you need, and if the value is always going to be the same, where you declare it you can put the value directly
private $variable_a_la_que_quiero_acceder = 5;
.As @BetaM correctly mentions in his comment, I think the problem here is a lack of basic object-oriented programming (OOP) in PHP. Laravel makes extensive use of OOP so I strongly recommend that you try to get a good foundation before continuing to use Laravel, otherwise you won't understand half of what you're doing and you'll have a lot of questions like this. For starters, W3Schools FREE php oop course is great, and will teach you the basics to get you up and running.