How laravel `Auth:user()` or `Auth:id()` works

GRESPL Nagpur picture GRESPL Nagpur · Feb 27, 2017 · Viewed 14.3k times · Source

How laravel Auth:user() or Auth:id() works

Is it resides in session or database.

I searched but not get good article.

Please help to understand. I know I will get many down-votes ;)

Answer

Rob picture Rob · Apr 4, 2018

Here's my attempt at figuring out what actually happens on an Auth::user() call:

Auth::user()

Illuminate\Support\Facades\Auth
extends Illuminate\Support\Facades\Facade

Facade::__callStatic('user')

static::getFacadeRoot()

resolveFacadeInstance(static::getFacadeAccessor == 'auth' (from Auth class))

return static::$app[$name];
static::$app is instance of Illuminate\Foundation\Application
extends Illuminate\Container\Container

which implements ArrayAccess (which is why $obj[] syntax works)

Container::offsetGet(auth)

Application::make(auth) 

Container::getAlias(auth) return 'auth'

Container::make(auth)

Container::resolve(auth)

yadda, yadda, yadda See in Application::registerCoreContainerAliases

'auth' = Illuminate\Auth\AuthManager

AuthManager::user() = AuthManager::__call = $this->guard()->user()

AuthManager::guard(web)

AuthManager::resolve(web) (see config/auth.php)

AuthManager::createSessionDriver() returns new Illuminate\Auth\SessionGuard

SessionGuard::user() // <---- this is what actually get's called, based on default config