douchu4048 2016-06-05 10:20
浏览 67
已采纳

如何从没有Auth的数据库中获取userId :()?

I want to get current userId from database without Auth::() and I don't want to get it from route like '/xxx/{id}'

Is there any other way to get the userId without Auth::()?

eg. $userId = User::where('id', $id)->pluck('id');

Here is my auth::controller declaration :

public function getLogout(Request $request)
    {

        $redis = \Redis::connection();
        $user = $request->user();

    if ($user) {
         $userID = $user->id;
      }
 //       $userId = User::where('id', $id)->pluck('id'); 
        dd($userID);
 //       $userId=Auth::user()->id;
        $userSessions = $redis->smembers('users:sessions:' . $userId);
        $currentSession = Session::getId();
        foreach ($userSessions as $sessionId) {
             if ($currentSession == $sessionId) {
          continue; 

                }
                 $redis->srem('users:sessions:' . $userId, $sessionId);
                $redis->del('laravel:' . $sessionId);

            }
        Auth::logout();
        return redirect()->route('main');
   }
  • 写回答

1条回答 默认 最新

  • dpmir1988 2016-06-05 10:36
    关注

    Directly from the docs:

    Alternatively, once a user is authenticated, you may access the authenticated user via an Illuminate\Http\Request instance. Remember, type-hinted classes will automatically be injected into your controller methods:

    <?php
    
    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    
    class ProfileController extends Controller
    {
        /**
         * Update the user's profile.
         *
         * @param  Request  $request
         * @return Response
         */
        public function updateProfile(Request $request)
        {
            if ($request->user()) {
                // $request->user() returns an instance of the authenticated user...
            }
        }
    }
    

    So, it would be something like this to get the ID:

    $user = $request->user();
    
    if ($user) {
        $userID = $user->id;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?