Search code examples
laravellaravel-4

How to "Refresh" the User object in Laravel?


In Laravel you can do this:

$user = Auth::user();

Problem is, if I do changes on items on that object, it will give me what was there before my changes. How do I refresh the object to get the latest values? I.e. To force it to get the latest values from the DB?


Solution

  • Laravel already does that for you. Every time you do Auth::user(), Laravel does

    // 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))
    {
        $user = $this->provider->retrieveByID($id);
    }
    

    It nulls the current user and if it is logged, retrieve it again using the logged id stored in the session.

    If it's not working as it should, you have something else in your code, which we are not seeing here, caching that user for you.