When you are working with laravel applications you may need to track users online activity such as are they currently logged in or not or how many user's are currently active. There are so many solution available in the internet also in ChatGPT 😏 by the way, however I found this approach which is returns more accurate result.

We are storing online user's activity in database and cache to get accurate result, here we are checking if there is any online user from the cache named isOnline.

/** * This will store activity when logout event is triggered * * @param LaravelEvents\Logout $event */ public function logout(LaravelEvents\Logout $event) { $this->fireAuthActivityEvent($event); } /** * Handle user's last seen status only * * @param LoginDetailEvent $event */ public function handleLastSeenStatus(LoginDetailEvent $event) { if (!\request()->ajax()) { if (Auth::check()) { $event->user->loginDetails()->update([ 'last_seen' => now(), 'type' => $event->user->roles->name ]); }} }
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners