如何在 Laravel 中“刷新”用户对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21751656/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 09:02:06  来源:igfitidea点击:

How to "Refresh" the User object in Laravel?

laravellaravel-4

提问by coderama

In Laravel you can do this:

在 Laravel 中,你可以这样做:

$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?

问题是,如果我对该对象上的项目进行更改,它会给我更改之前的内容。如何刷新对象以获取最新值?即强制它从数据库中获取最新值?

采纳答案by Antonio Carlos Ribeiro

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

Laravel 已经为你做到了。每次你这样做时Auth::user(),Laravel 都会这样做

// 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.

它将当前用户归零,如果它被记录,则使用存储在会话中的记录的 id 再次检索它。

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.

如果它不能正常工作,则您的代码中还有其他内容,我们在这里没有看到,为您缓存该用户。

回答by Er. Mohit Agrawal

You can update the cache object like this.

您可以像这样更新缓存对象。

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'

回答by Yahya Uddin

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

[这个答案更适合新版本的 Laravel(即 Laravel 5)]

On the first call of Auth::user(), it will fetch the results from the database and store it in a variable.

在第一次调用 时Auth::user(),它将从数据库中获取结果并将其存储在一个变量中。

But on subsequent calls it will fetch the results from the variable.

但是在随后的调用中,它将从变量中获取结果。

This is seen from the following code in the 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 on the model, the changes will automatically be reflected on the object. It will NOT contain the old values. Therefore there is usuallyno need to re-fetch the data from the database.

现在,如果我们对模型进行更改,更改将自动反映在对象上。它不会包含旧值。因此通常不需要从数据库中重新获取数据。

However, there are certain rare circumstances where re-fetching the data from the database would be useful (e.g. making sure the database applies it's default values, or if changes have been made to the model by another request). To do this run the fresh()method like so:

然而,在某些罕见的情况下,从数据库重新获取数据会很有用(例如,确保数据库应用它的默认值,或者如果另一个请求对模型进行了更改)。为此,请运行如下fresh()方法:

Auth::user()->fresh()

回答by Jon

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

Laravel 确实为您做到了这一点,但是,在同一请求期间,您不会看到 Auth::user() 中反映的更新。来自 /Illuminate/Auth/Guard.php(位于 Antonio 在他的回答中提到的代码上方):

// 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 were trying to change the users name 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 try getting the name by checking Auth::user()->name, its going to give you 'Old Name'

稍后在同一个请求中,您尝试通过检查获取名称Auth::user()->name,它将为您提供“旧名称”

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

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

回答by GTCrais

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

聚会有点晚了,但这对我有用:

Auth::user()->update(array('name' => 'NewName'));