How to "Refresh" a user object in Laravel?

In Laravel you can do this:

$user = Auth::user(); 

The problem is that if I make changes to the elements of this object, it will give me what it was before my changes. How to update an object to get the latest values? That is, to make it get the latest values ​​from the database?

+8
laravel laravel-4
source share
5 answers

Laravel is already doing this 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 resets the current user and, if he registers, retrieves him again using the registered identifier stored in the session.

If it does not work properly, you have something else in the code that we do not see here, caching this user for you.

+1
source share

You can update the cache object as follows.

 Auth::setUser($user); 

for example

 $user = User::find(Auth::user()->id); $user->name = 'New Name'; $user->save(); Auth::setUser($user); log::error(Auth::user()->name)); // Will be 'NEW Name' 
+8
source share

Laravel does this for you, HOWEVER, you will not see this update reflected in Auth :: user () during the same request. From /Illuminate/Auth/Guard.php (located just above the code that Antonio mentions in his answer):

 // 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; } 

So, if you tried to change the username from "Old Name" to "New Name":

 $user = User::find(Auth::user()->id); $user->name = 'New Name'; $user->save(); 

And later in the same request, you will try to get the name by checking Auth::user()->name so that it gives you the "Old Name"

log::error(Auth::user()->name)); // Will be 'Old Name'

+5
source share

[This answer is more suitable for newer versions of Laravel (namely Laravel 5)]

The first time Auth::user() called, it will retrieve the results from the database and save them in a variable.

But on subsequent calls, it will extract the results from the variable.

This can be seen from the following code in framemwork:

 public function 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; } ... } 

Now, if we make changes to the model, the changes will automatically reflect on the object. It will not contain old values. Therefore, it is usually not necessary to retrieve data from the database.

However, there are some rare circumstances in which it would be useful to reuse data from the database (for example, make sure the database uses the default values ​​or if changes were made to the model by another request). To do this, run the fresh() method as follows:

 Auth::user()->fresh() 
+2
source share

A little late to the party, but it worked for me:

 Auth::user()->update(array('name' => 'NewName')); 
+1
source share

All Articles