1

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?

M'Baku
  • 320
  • 4
  • 18
  • 1
    Share your data from a Service Provider: https://stackoverflow.com/a/28609372/397195 – Marwelln Mar 04 '18 at 11:32
  • It looks like the first version of your code could work, depending on the definition of the `User` class which is missing from your example. – RWRkeSBZ Mar 04 '18 at 17:52

0 Answers0