Pass a variable with unique value to another class

0

I have two classes

class class1 extends Controller
{

    public function fun 1($xxxxxx){

             $sCliente = new Client;
                $sCliente->xxxx = $oData->xxx->id;
                $sCliente->name = xxx->fullName;
                $sCliente->xxxx = xxxx->email;
                $sCliente->id_usuario = $aClient['xxxxx'];
                $sCliente->save();

          $oIdCliente = $sCliente->id; // esto es para obtener el id 
    }
}

and this is my other class

class class2 extends Controller
{
    public function fun2($xxx){

            $oIdClient = $this->ctrCliente()->createUser();

    }
}

What I'm trying to do is go through $oIdCliente = $sCliente->id; pass id value of create to the other class but I can not get it to be

    
asked by Jhon Bernal 12.08.2018 в 00:55
source

2 answers

1

Your code and your explanation are confusing because in your first class, you instantiate an object of the Client class and we do not know where it comes from like in the second class, you are calling a method of the class that we do not know if it exists, what it does, etc.

Well, to access actions or properties from controller to controller, you should not, you would not be following the Laravel pattern, if you need to pass values or use methods from one controller to another controller you should consider doing a re-factor of your code and create a service class.

Your structure would look something like this:

class UsersService
{
  public function getId()
  {
    // tu implementación aquí.
  }
}

//No olvides importar la clase servicio antes
class Clase1 extends Controller
{
  protected $userService;
  public function __construct(UsersService $userService)
  {
     $this->userService = $userService;
  }

  public function create() 
  {
    // llamar al método
    $this->userService->getId();
    // lo demás acá
  }
}
    
answered by 15.08.2018 / 18:24
source
0

If you need to create different files with classes, your best option is to use the traits.

Example In my file casePrincipal.php I have

namespace \App;
class clasePrinsipal extends Controller {
     use \App\trait;
     function funcion1(){
         $this->function2($valor);
     }
}

Now building your trait would look like this.

 namespace \App;
    trait masclases {

         function funcion2($valor){
             Aqui haces loq ue tengas que hacer con el valor que mandas 
             desde la primera funcion

         }
    } 

Do not forget that depending on the folder structure the namespace is built

    
answered by 19.08.2018 в 11:23