Determine User Online Status in Laravel

Last modified on October 16, 2020 2 min read

We can easily determine the user is online or not. In this article, we will learn about how to detect a user is online or offline. I’m testing on Laravel 5.8. Let’s start:

Note: I’ve written a new article. Please take a look: How to Determine User Online Status & Last Seen in Laravel.

Table of Contents

  1. Install Laravel and Basic Configurations
  2. Create a Middleware
  3. Add Middleware to Kernel
  4. Check Online Status in Controller
  5. Check Online Status in Balde File

Step 1 : Install Laravel and Basic Configurations

Each Laravel project needs this thing. That’s why I have written an article on this topic. Please see this part from here: Install Laravel and Basic Configurations.

After completing the basic configurations, run this command to generate Laravel primary authentication:

php artisan make:auth

Now migrate the migration:

php artisan migrate

Step 2 : Create a Middleware

Create a middleware named LastUserActivity by typing this command:

php artisan make:middleware LastUserActivity

Now open the middleware LastUserActivity.php from app>Http>Middleware and paste this code:


namespace App\Http\Middleware;

use Closure;
use Auth;
use Cache;
use Carbon\Carbon;

class LastUserActivity
     * Handle an incoming request.
     * @param \Illuminate\Http\Request $request
     * @param \Closure $next
     * @return mixed
    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);

Step 3 : Add Middleware to Kernel

Go to app>Http and open Kernel.php. We have to add \App\Http\Middleware\LastUserActivity::class,this line to $middlewareGroups.

 * The application's route middleware groups.
 * @var array
protected $middlewareGroups = [
    'web' => [
        // \Illuminate\Session\Middleware\AuthenticateSession::class,

    'api' => [

Step 4 : Check Online Status in Controller๏ปฟ

We have done all the tasks. Now we can check the online status in the controller:

Create a controller named UserController:

php artisan make:controller UserController

Open the controller from app>Http>Controllers and paste this code:


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use DB;
use Cache;

class UserController extends Controller

     * Show user online status.
    public function userOnlineStatus()
        $users = DB::table('users')->get();
        foreach ($users as $user) {
            if (Cache::has('user-is-online-' . $user->id))
                echo "User " . $user->name . " is online.
"; else echo "User " . $user->name . " is offline.
"; } } }

Open routes>web.php to create a route:

Route::get('/check', '[email protected]');

Now run the project by typing php artisan serve and visit the route to see the output.

Step 5 : Check Online Status in Blade File

We can also easily show online status in blade file. Here’s the syntax:

@if(Cache::has('user-is-online-' . $user->id))
    <span class="text-success">Online</span>
    <span class="text-secondary">Offline</span>

Let’s see a full example. From resources>views, open home.blade.php and paste this code:


    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">Users</div>

                    <div class="card-body">

                        @php $users = DB::table('users')->get(); @endphp

                        <div class="container">
                            <table class="table table-bordered">
                                @foreach($users as $user)
                                            @if(Cache::has('user-is-online-' . $user->id))
                                                <span class="text-success">Online</span>
                                                <span class="text-secondary">Offline</span>


Now login to your Laravel project to see the output. Here’s mine:

Determine User Online Status in Laravel
We have completed the project. Thank you. ๐Ÿ™‚

Monthly Newsletter

One email a month, packed with the latest tutorials, delivered straight to your inbox.
We'll never send any spam or promotional emails.

Hey, I'm Md Obydullah. I build open-source projects and write on Laravel, Linux server, modern JavaScript and more on web development.


36 Replies to “Determine User Online Status in Laravel”

  1. Hi Dear,

    I had contiune from step 2. (Because I have an app).

    It does not work on my Laravel 6.0.

    It shows every member is offline. But I’m online?

    What could be reason? and what is the working logic for this method? How it determines they are online/offline and put in the cache?

    Many thanks.

    1. Hi Yusril, you can keep the last seen time. In the handle() function of LastUserActivity.php you can add a line like this:

      User::where('id', Auth::user()->id)->update(['last_seen' => (new \DateTime())->format("Y-m-d H:i:s")]);

      I’ll also publish an article today on this topic. You can check later.

    1. Hello Jeeva, In the middleware, I set option to store last user online time for 1 minute. I saved the data in the cache. So, the cache needs to expire at current time + addMinutes(1). That’s it.

      And yeah, you can use addSeconds(10).

Leave a Reply