See Who’s Online in your laravel app

In this article i will show you how to  See Who’s Online in your laravel app using Laravel Package called activity With this package, you can easily see who’s online and how many guests are viewing your site. so lets get started with installation


To install this package, just follow these quick few steps.


As always, pull this package through composer by opening composer.json file and adding this within require:

"kim/activity": "~1.0"

Afterward, run either composer update or composer install.

Providers and Aliases

Next, open config/app.php and add this to your providers array:


and this to your aliases array:

'Activity' => KimActivityActivityFacade::class

Session and Database Setup

Finally, you need to change your session configuration to use the database. Open the .env file, which should be at the root directory of your Laravel project. Then, change your session driver to database.


Finally, publish the migration file and run it.

php artisan vendor:publish

php artisan migrate


This package will automatically update a user’s or guest’s most recent activity. To grab the most recent users and guests though, you can use the easy-to-use built-in methods.

Grabbing Most Recent Activities

Import the Activity facade at the top and then do a simple query.

// Import at the top
use Activity;

// Find latest users
$activities = Activity::users()->get();

// Loop through and echo user's name
foreach ($activities->users as $user) {
    echo $user->name . '<br>';

The users method will grab the most recent activities within the past 5 minutes. You can change the default timespan by specifying the minutes.

$activities = Activity::users(1)->get();   // Last 1 minute
$activities = Activity::users(10)->get();  // Last 10 minutes
$activities = Activity::users(60)->get();  // Last 60 minutes

You have other methods for your convenience to grab the latest activities by seconds or even hours.

$activities = Activity::usersBySeconds(30)->get();  // Get active users within the last 30 seconds
$activities = Activity::usersByMinutes(10)->get();  // Get active users within the last 10 minutes
$activities = Activity::usersByHours(1)->get();     // Get active users within the last 1 hour

$numberOfUsers = Activity::users()->count();        // Count the number of active users

Sorting Methods

In order to sort the activities by most and least recent, just use the mostRecent and leastRecent methods.

$activities = Activity::users()->mostRecent()->get();   // Get active users and sort them by most recent
$activities = Activity::users()->leastRecent()->get();  // Get active users and sort them by least recent

In addition to this, you can sort the user’s attributes by using the orderByUsers method. For example, rather than ordering by the most recent activity, lets say you want to order by the users’ name alphabetically. You can do this.

$activities = Activity::users()->orderByUsers('email')->get();

Grabbing the Guests

In order to grab the number of guests that are online, it’s just as intuitive as grabbing the users. For example:

$numberOfGuests = Activity::guests()->count();      // Count the number of active guests

About the author

Deven Rathore

I'm Deven Rathore, a multidisciplinary & self-taught designer with 3 years of experience. I'm passionate about technology, music, coffee, traveling and everything visually stimulating. Constantly learning and experiencing new things.

  • Haneef Ansari


    • Deven Rathore


  • Divo

    Cool. What if your session driver is a file? Can this work (my current code)? Please what is the advantage of using a database session driver over a file. Lastly, if you have two laravel apps using thesame database, how do you deal with the session table? Appreciate help. Thanks again for this post.

    • Deven Rathore

      Read this article for more info

      you can save them in the database,but there no need to create separate column for that. for example It goes in to the userdata column

      You set the username by $this->session->set_userdata(‘sessionname’,session value);

      You can retrieve it by $var=$this->session->userdata(‘sessionname’);

      • Divo

        Thanks Deven. I have read the article. Pretty interesting. Have shared it. I am not using Master-Slave configuration. I just have two apps using same DB. Both apps have authentication. Currently, I am using a file as session driver in both apps. I am just wondering if there is way to know who is online when the session driver is a file. My problem with using database is because both apps would be using same table which won’t give me accurate data. One way I can think of is make one app use file and the other (which I want to display number of connected users) use database as session driver. Is they any better approach to achieve this? thanks

Pin It on Pinterest