How can I get the value of this variable?

无人久伴 提交于 2019-12-24 22:03:17

问题


In logout function, I need to get the value of $time. This variable was declared in other function, when user logged in.

Broadcast::channel('chat', function ($user) {
        $ip = Request::ip();
        $time = now();  
        if (auth()->check() && !session()->has('name'))  {  
            UserInfo::storeUser();
            session()->put('name',$user->name);
            return [
                'id' => $user->id,
                'ip' => $ip,
                'name' => $user->name,
                'joined' => $time,                   
            ];
        }
    });

When user logged out, I have to check his 'id' and value of 'joined'. How can I do this?

public function logout() {
       $id = auth()->id();
       $info = \App\UserInfo::where('id', $id)
                             -> where('joined', $time)
                             ->update(['left' => now()]);
        auth()->logout();
        session()->forget('name');
        session()->put('left',now());
        return redirect('/');
    }

回答1:


You must use a primary unique key as id and make a column 'user_id' for storing the auth()->id in the table. While joining/starting log in session, store the 'id' in application as 'session_id'.

You can get the 'joined' value by using id as follows,

$joined = \App\UserInfo::where('id', auth()->session_id)
          ->select('joined')
          ->first()
          ->joined;

At end of session, update the left details with the id as follows,

 $info = \App\UserInfo::where('id', auth()->session_id)
            ->update(['left' => now()]);


来源:https://stackoverflow.com/questions/58146967/how-can-i-get-the-value-of-this-variable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!