检查用户是否在线 laravel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50075968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if user online laravel
提问by Jadasdas
I have column last_activity, where write date last activity user with middleware.
How I can check online user and when he logout?
我有列 last_activity,其中使用中间件写入最后一个活动用户的日期。
我如何检查在线用户以及他何时退出?
Middleware:
中间件:
class LastActivityUser
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if (\Auth::check() && (\Auth::user()->last_activity < new \DateTime('-5 minutes'))) {
$user = \Auth::user();
$user->last_activity = new \DateTime;
$user->timestamps = false;
$user->save();
}
return $next($request);
}
}
Code in User model:
用户模型中的代码:
public function online() {
return ($this->last_activity > new \DateTime('-5 minutes') && $user->check()) ? true : false;
}
$user->check
=> Auth::check()
- not working. I don't need use Auth::check(), I need show online user on other users.. But Auth::check()
check if user stay in account only for current auth user..
$user->check
=> Auth::check()
- 不工作。我不需要使用 Auth::check(),我需要在其他用户上显示在线用户.. 但是Auth::check()
检查用户是否只保留当前 auth 用户的帐户..
采纳答案by Praveen Tamil
<?php
namespace App\Http\Middleware;
use Closure;
use Illuminate\Contracts\Auth\Factory as Auth;
class LastActivityUser
{
/**
* The authentication factory instance.
*
* @var \Illuminate\Contracts\Auth\Factory
*/
protected $auth;
/**
* Create a new middleware instance.
*
* @param \Illuminate\Contracts\Auth\Factory $auth
* @return void
*/
public function __construct(Auth $auth)
{
$this->auth = $auth;
}
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
public function handle($request, Closure $next)
{
if ($this->auth->check() && $this->auth->user()->last_activity < Carbon::now()->subMinutes(5)->format('Y-m-d H:i:s')) {
$user = $this->auth->user();
$user->last_activity = new \DateTime;
$user->timestamps = false;
$user->save();
}
return $next($request);
}
}
回答by Mohammad b
you could use middleware to check online users
您可以使用中间件来检查在线用户
if(Auth::check()) {
$expiresAt = Carbon::now()->addMinutes(5);
Cache::put('user-is-online-' . Auth::user()->id, true, $expiresAt);
}
and check users:
并检查用户:
public function isOnline()
{
return Cache::has('user-is-online-' . $this->id);
}
in view:
鉴于:
@if($user->isOnline())
user is online!!
@endif
maybe this document will help you : https://erikbelusic.com/tracking-if-a-user-is-online-in-laravel/
也许这份文件会帮助你:https: //erikbelusic.com/tracking-if-a-user-is-online-in-laravel/
回答by Mahdi
you can use below package in order to get a list of online users or determine if a user is online or not.
您可以使用以下包来获取在线用户列表或确定用户是否在线。