dimanche 4 mars 2018

Instatiate class in a global variable

I am trying to instantiate the user class in a global variable, so I don't have to do that in every method I want to return the user details.

class showMore{

 public $user;

public function __construct(){

    $this->user = new User;
    $this->user = $this->user->where('id', Auth::id())->firstOrfail();
}

public function index()
{


   return view('student.app.index', ['user'=>$this->user]);
}
}

instead of this

 class showMore{

public $user;

public function __construct(){

    $this->user = new User;
    $this->user = $this->user->where('id', Auth::id())->firstOrfail();
}
public function index()
{


   $user = new User;
   $user = $user->where('id', Auth::id())->fistOrFail();
   return view('student.app.index', ['user'=>$user]);
}
public function indexMore()

{

   $user = new User;
   $user = $user->where('id', Auth::id())->fistOrFail();
   return view('student.app.index', ['user'=>$user]);
}
}

How do I go about making the proper change?



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2oHyr1A
via IFTTT

Aucun commentaire:

Enregistrer un commentaire