User reverse current in Laravel 4

Using PHP and Laravel 4 I have a method in my User model, as shown below, to test the Admin user ...

public function isAdmin() { if(isset($this->user_role) && $this->user_role === 'admin'){ return true; }else{ return false; } } 

This does not work if I call this function in other classes or models.

To get the desired result, I had to do it like this ...

 public function isAdmin() { if(isset(Auth::user()->user_role) && Auth::user()->user_role === 'admin'){ return true; }else{ return false; } } 

I am trying to access this inside my Admin Controller, as shown below, but it returns an empty User object instead of the currently logged in User Object ...

 public function __construct(User $user) { $this->user = $user; } 

So my question is: how can I get the first version to work? When I create an instance of the User object in another class, I need to somehow make sure that it has data for the currently logged-in user, but I'm not sure if this is the best way ... I know that this is basic. I'm just a little rusty right now can use help, thanks

+7
php laravel laravel-4
source share
1 answer

Returns a user repository - not the current registered user

 public function __construct(User $user) 

To access the current registered ANYWHERE user in your application - just do

 Auth::user() 

(as your average example)

So, therefore - to check if the user is the ANYWHERE admin user in your application - just do

 if (Auth::user()->isAdmin()) { // yes } else { // no } 
+14
source share

All Articles