【问题标题】:How can I get the value of this variable?我怎样才能得到这个变量的值?
【发布时间】:2019-09-28 13:51:26
【问题描述】:

在注销功能中,我需要获取 $time 的值。这个变量是在用户登录时在其他函数中声明的。

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,                   
            ];
        }
    });

当用户退出时,我必须检查他的 'id' 和 'joined' 的值。我该怎么做?

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('/');
    }

【问题讨论】:

    标签: php laravel laravel-5 eloquent


    【解决方案1】:

    您必须使用 主唯一键 作为 id 并创建一个列 'user_id' 用于在表中存储 auth()->id。在加入/开始登录会话时,将应用程序中的“id”存储为“session_id”。

    您可以通过使用 id 获取 'joined' 值,如下所示,

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

    在会话结束时,使用 id 更新左侧的详细信息,如下所示,

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

    【讨论】:

    • 我得到错误 Undefined property: Illuminate\Auth\AuthManager::$session_id
    • 实际上,我是按照自己的方式做的,但最后一切正常。这个关于主键和外键的信息也非常有用。 stackoverflow.com/questions/58145745/…
    猜你喜欢
    • 1970-01-01
    • 2022-01-06
    • 1970-01-01
    • 2021-06-07
    • 1970-01-01
    • 1970-01-01
    • 2018-02-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多