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

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners