Laravel get online users code example
Example: how check the online user in laravel
//1 - the first step make middleware
public function handle($request, Closure $next)
{
if(Auth::check()) {
$expiresAt = Carbon::now()->addMinutes(1);
Cache::put('user-is-online-' . Auth::user()->id, true, $expiresAt);
}
return $next($request);
}
// the second step is add a class into Kernel
//the third step is add a function into the User Model
public function isOnline()
{
return Cache::has('user-is-online-' . $this->id);
}
//the last step is check user Online or offline in Laravel application
@if($user->isOnline())
user is online!!
@endif