Answers for "how check the online user in laravel"

PHP
0

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
Posted by: Guest on February-02-2021

Code answers related to "how check the online user in laravel"

Browse Popular Code Answers by Language