Join the Stack Overflow Community
Stack Overflow is a community of 6.3 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

In my Laravel application I used Auth::user() in multiple places. I am just worried that Laravel might be doing some queries on each call of Auth::user()

Kindly advice

share|improve this question
up vote 8 down vote accepted

No the user model is cached. Let's take a look at Illuminate\Auth\Guard@user:

public function user()
{
    if ($this->loggedOut) return;

    // If we have already retrieved the user for the current request we can just
    // return it back immediately. We do not want to pull the user data every
    // request into the method because that would tremendously slow an app.
    if ( ! is_null($this->user))
    {
        return $this->user;
    }

As the comment says, after retrieving the user for the first time, it will be stored in $this->user and just returned back on the second call.

share|improve this answer
    
Please note that this is not persistent between different requests. What I mean is that each time there is a new request (e.g: User refreshes the page), regardless of the inner caching, the driver goes to database every time. – Arda Sep 2 '15 at 20:04

For same Request, If you run Auth::user() multiple time, it will only run 1 query and not multiple time. But , if you go and call for another request with Auth::user() , it will go and run 1 query again.

This cannot be cached for all request after first request has been made due to security point of view.

I see use of some session here , http://www.rebootcode.com/php/laravel/using-session-against-authuser-in-laravel-4-and-5-cache-authuser/

Thanks

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.