在 Laravel 中获取 Auth 用户 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33486838/
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 12:37:07  来源:igfitidea点击:

Getting Auth User ID in Laravel

phpsessionlaravel

提问by Cihan Küsmez

If a user logs in Laravel 5.1 we can access user id

如果用户登录 Laravel 5.1,我们可以访问用户 ID

Auth::user()->id

In my previous app (not laravel) when a user logs in I'm registering a session for userid. And I was checking $_SESSION['user_id'] is available or not.

在我以前的应用程序(不是 laravel)中,当用户登录时,我正在为 userid 注册一个会话。我正在检查 $_SESSION['user_id'] 是否可用。

I want to ask that when i call Auth::user()->idis it generates and sql query for every request ? If it does it is not good for performance.

我想问一下,当我打电话时Auth::user()->id,它是否为每个请求生成和 sql 查询?如果这样做,则对性能不利。

Should I register a new session like

我应该注册一个新的会话吗

Session::put('user_id', Auth::user()->id);

for performance.

为了性能。

Or Auth:user()->id is best choice ?

或者 Auth:user()->id 是最好的选择?

回答by Thomas Kim

You can use Auth::id()instead. This grabs it from the session. If one doesn't exist in the session, then it will run the query and grab it from the database.

你可以Auth::id()改用。这是从会话中获取的。如果会话中不存在,那么它将运行查询并从数据库中获取它。

回答by M.S. Dousti

This is not an actual answer, but it intends to show how Auth::id()works. Add the following route to your web.php:

这不是一个实际的答案,但它旨在展示如何Auth::id()工作。将以下路由添加到您的web.php:

Route::get('/fake', function () {
  \DB::enableQueryLog();
  Auth::id();
  dump(\DB::getQueryLog());
});

Now, navigate to /fake, which results in the something like:

现在,导航到/fake,结果如下:

array:1 [▼
  0 => array:3 [▼
    "query" => "select * from `users` where `id` = ? limit 1"
    "bindings" => array:1 [▼
      0 => 284
    ]
    "time" => 15.37
  ]
]

I tried this over and over, and it seems that Auth::id()always queries the database, as opposed to somehow caching the result in the session (I'm using Laravel 5.6, PHP 7.2.3, and MariaDB 10.2.14).

我一遍又一遍地尝试,似乎Auth::id()总是查询数据库,而不是以某种方式在会话中缓存结果(我使用的是 Laravel 5.6、PHP 7.2.3 和 MariaDB 10.2.14)。

To confirm, I also enabled MariaDB logging feature, and I got:

为了确认,我还启用了MariaDB 日志记录功能,我得到了:

180611 12:08:10    77 Connect   user@localhost as anonymous on dbname
           77 Query use `dbname`
           77 Prepare   set names 'utf8mb4' collate 'utf8mb4_unicode_ci'
           77 Execute   set names 'utf8mb4' collate 'utf8mb4_unicode_ci'
           77 Close stmt    
           77 Prepare   set time_zone="+04:30"
           77 Execute   set time_zone="+04:30"
           77 Close stmt    
           77 Prepare   set session sql_mode='NO_ENGINE_SUBSTITUTION'
           77 Execute   set session sql_mode='NO_ENGINE_SUBSTITUTION'
           77 Close stmt    
           77 Prepare   select * from `users` where `id` = ? limit 1
           77 Execute   select * from `users` where `id` = 284 limit 1
           77 Close stmt    
           77 Quit