Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 409
Prakhar Avatar asked Oct 16 '16 00:10

Prakhar


People also ask

What is auth ()- user () in Laravel?

Auth::user() — You can check if a user is authenticated or not via this method from the Auth Facade. It returns true if a user is logged-in and false if a user is not. Check here for more about how Facades work in Laravel.

How does Laravel Auth attempt work?

The attempt method accepts an array of key / value pairs as its first argument. The password value will be hashed. The other values in the array will be used to find the user in your database table. So, in the example above, the user will be retrieved by the value of the email column.

How can check user is active or not in Laravel?

Works on Laravel 7 * * @return void */ public function boot() { Fortify::authenticateUsing(function (Request $request) { $user = User::where('email', $request->email)->where('status', 1)->first(); if ($user && Hash::check($request->password, $user->password)) { return $user; } }); // ... }


2 Answers

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.

like image 80
bytesarelife Avatar answered Oct 03 '22 11:10

bytesarelife


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.

like image 35
aleksejjj Avatar answered Oct 03 '22 10:10

aleksejjj