How to pass a variable from one function to another in laravel?

1

Best regards, colleagues, I have the following doubt regarding this, suppose I have the following functions in one of my controllers:

public function DatosEstudio(Request $request){

$ConsultaActividades=DB::table('actividades')->select('nombre_actividad')->where('nombre_actividad','like','1%')->get();

}

 public function Recibir(Request $request){


}

How could the QueryActivities variable pass to the other function (Receive) that is in the same controller? In the receive function I also use request since this function must also get some data by form. I am trying it in the following way.

public function Recibir(Request $request)
{

   $var= $this->DatosEstudio($consultaActividades);
return $consultaActividades;

}  

But I miss the error, how should this procedure be done correctly?

    
asked by Kevin Burbano 29.04.2018 в 20:37
source

2 answers

0

It is not correct for design conventions to call another method that includes a Request, in that order of ideas the two methods should be communicated by another method private or protected, or going a little further, by a model, repository or service , which is called from both methods, or depending on what you intend to do, pass part of the code of the second method to another more abstract layer as well.

The first option is summarized in:

protected function getData()
{
    return DB::table('actividades')->select('nombre_actividad')->where('nombre_actividad','like','1%')->get();
}


public function Recibir(Request $request) {
    $var = $this->getData();
    // ...
} 
    
answered by 29.04.2018 / 21:53
source
1

In your case there are two operators to refer to this, the object operator and the static, both are defined outside the method. a simple example would be

class SayIt
{

 public $var = NULL;

  public function getData($var)
  {
    return $this->var = $var;
  }

}
// ahora podemos acceder al objeto desde otro archivo previamente llamando al archivo y su localización 
$op = new SayIt();
$op->getData("My name is Outman");
echo $op->var;

In this case we have used the object operator, see more details here

    
answered by 30.04.2018 в 01:06