I have column last_activity, where write date last activity user with middleware.
How I can check online user and when he logout?
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..
You can go this way. Go to Auth/LoginController and add this line. For this to work you have to have a column named 'status' in users table. 1 is for active and 0/2 is for inactive user.
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/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With