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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 pb数据库修改或者求完整pb库存系统,需为pb自带数据库
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错
  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路