Skip to content

PHP code snippet – 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
See also  How to save plot in Python?

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.