Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

aktolman's avatar

Using the Auth to get current user data.

I would like to get the current logged in user record so I can retrieve a language id from the database and thus call my language function to get language tokens.

I have seen lots of people saying user Auth::user()->fieldName to get the field however when I try this I just get the following error:

Class 'App\Auth' not found

I am going to need easy access to this throughout my application so I have a couple of questions:

  1. What location is this? so what would be the "use blah/blah/Auth"?
  2. How do make it so this is always available without having to declare it?

Apologies if I am being thick - I am quite new to Laravel...

Cheers!

0 likes
11 replies
bestmomo's avatar
Level 52

There is an helper :

auth()->user()...

You can use it anywhere without import or injection.

7 likes
martinbean's avatar

@aktolman You could get the currently logged in user in your base controller:

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Contracts\Auth\Guard;

abstract class Controller extends BaseController
{
    use DispatchesJobs, ValidatesRequests;

    public function __construct(Guard $auth)
    {
        $this->currentUser = $auth->user();
    }
}

Note: you will need to call parent::__construct() in any extending controller classes.

3 likes
martinbean's avatar

@bimalshah72 It’s a matter of preference. I personally like to type-hint dependencies in my classes/methods than use magic façades.

1 like
bestmomo's avatar

@martinbean

I think helper auth() is simpler and achieve the same code. It's as if you write :

$currentUser = app('Illuminate\Contracts\Auth\Guard')->user();
2 likes
aktolman's avatar

Awesome thanks everyone! really handy answers here! already put to good use!

martinbean's avatar

@bestmomo I prefer to follow best practices and not have too much “magic” going on in my code. Makes re-factoring easier too, rather than hinging code on façades and global helper functions.

bestmomo's avatar

@martinbean if you have injections in abstract controller you cant just call parent::__construct() because you need parameters.

grafix's avatar

@martinbean, Sir I tried your solution. But when I call in the view I got an error. I tried to call like this currentUser variable.

Please or to participate in this conversation.