Laravel 5 : Does Auth::user() query the database everytime I use it?
Asked Answered
T

2

12

On the edit profile page for a user, I want to show the existing values of the current logged-in user details like name, email, gender etc. My questions are as follows

  1. Is it recommendable to user Auth::user()->name , Auth::user()->email directly to populate the form fields ? Or shall I create a variable like $user = Auth::user(); in my controller and pass it on to my view to $user like a regular object?

  2. Does using Auth::user(), multiple times on a given view file hit my database each time I use it?

    Thanks in advance.

Tavish answered 16/10, 2016 at 0:9 Comment(1)
once authenticated on the LoginController, you can put these Auth::user() variables on a session, then just use these session variables anywhere.Ingram
S
15

If you look at the SessionGuard.php file in Illuminate\Auth, you'll see the method user() which is used to retrieve the currently authenticated user:

/**
 * Get the currently authenticated user.
 *
 * @return \Illuminate\Contracts\Auth\Authenticatable|null
 */
public function user()
{
    if ($this->loggedOut) {
        return;
    }

    // If we've already retrieved the user for the current request we can just
    // return it back immediately. We do not want to fetch the user data on
    // every call to this method because that would be tremendously slow.
    if (! is_null($this->user)) {
        return $this->user;
    }

    $id = $this->session->get($this->getName());

    // First we will try to load the user using the identifier in the session if
    // one exists. Otherwise we will check for a "remember me" cookie in this
    // request, and if one exists, attempt to retrieve the user using that.
    $user = null;

    if (! is_null($id)) {
        if ($user = $this->provider->retrieveById($id)) {
            $this->fireAuthenticatedEvent($user);
        }
    }

    // If the user is null, but we decrypt a "recaller" cookie we can attempt to
    // pull the user data on that cookie which serves as a remember cookie on
    // the application. Once we have a user we can return it to the caller.
    $recaller = $this->getRecaller();

    if (is_null($user) && ! is_null($recaller)) {
        $user = $this->getUserByRecaller($recaller);

        if ($user) {
            $this->updateSession($user->getAuthIdentifier());

            $this->fireLoginEvent($user, true);
        }
    }

    return $this->user = $user;
}

// If we've already retrieved the user for the current request we can just return it back immediately. We do not want to fetch the user data on every call to this method because that would be tremendously slow.

    if (! is_null($this->user)) {
        return $this->user;
    }

So, calling the user() multiple times won't make multiple calls to the database.

Schweitzer answered 16/10, 2016 at 2:46 Comment(0)
B
2

You'll get only 1 request to database, so using Auth::user() multiple times is not a problem.

I recommend you using Laravel Debugbar as the most comfortable way for app optimization.

Barrows answered 16/10, 2016 at 0:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.