问题
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