每次我调用Auth :: user()时,Laravel都会查询数据库吗? [英] Does Laravel query database each time I call Auth::user()?

查看:285
本文介绍了每次我调用Auth :: user()时,Laravel都会查询数据库吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Laravel应用程序中,我在多个地方使用了Auth::user().我只是担心Laravel可能会在每次Auth::user()

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()

请忠告

推荐答案

不缓存用户模型.让我们来看看Illuminate\Auth\Guard@user:

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

正如评论所说,第一次检索用户后,它将存储在$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.

这篇关于每次我调用Auth :: user()时,Laravel都会查询数据库吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆