How to Determine User Online Status & Last Seen in Laravel 6+

Last modified on June 11, 2020 3 min read

in Laravel, 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 and store last seen data. I’m testing on Laravel 6.16.0. Let’s start:

Table of Contents

  1. Install Laravel and Basic Configurations
  2. Generate Auth Scaffolding
  3. Modify User Table
  4. Create a Middleware
  5. Add Middleware to Kernel
  6. Check Online Status in Controller
  7. Check Online Status in Blade File

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.

Generate Auth Scaffolding

We’ve to enable auth scaffolding. Run these commands to enable auth scaffolding:

# install laravel ui
composer require laravel/ui --dev

# auth scaffolding
php artisan ui vue --auth

# finally run
npm i && npm run dev

Done, we’ve enabled login/register options.

Modify User Table

We need to add one column in the users table called last_seen. Open users table migration file from database>migrations folder and in the up() functions add like this:

public function up()
    Schema::create('users', function (Blueprint $table) {

Now migrate the migrations:

php artisan migrate

For an existing project, you can just add last_seen column to the users table.

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 App\User;
use Closure;
use Auth;
use Cache;
use Carbon\Carbon;

class LastUserActivity
    public function handle($request, Closure $next)
        if (Auth::check()) {
            $expiresAt = Carbon::now()->addMinutes(1); // keep online for 1 min
            Cache::put('user-is-online-' . Auth::user()->id, true, $expiresAt);

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

        return $next($request);

In the middleware, we’re checking the user is active or not.

Add Middleware to Kernel

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

protected $middlewareGroups = [
    'web' => [
        // \Illuminate\Session\Middleware\AuthenticateSession::class,

    'api' => [

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 App\User;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Cache;

class UserController extends Controller

     * Show user online status.
    public function userOnlineStatus()
        $users = User::all();

        foreach ($users as $user) {
            if (Cache::has('user-is-online-' . $user->id))
                echo $user->name . " is online. Last seen: " . Carbon::parse($user->last_seen)->diffForHumans() . " <br>";
                echo $user->name . " is offline. Last seen: " . Carbon::parse($user->last_seen)->diffForHumans() . " <br>";

Open routes>web.php and create a route:

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

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

Check Online Status in Blade File

We can also easily show online status in the 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">
                                    <th>Last Seen</th>
                                @foreach($users as $user)
                                            @if(Cache::has('user-is-online-' . $user->id))
                                                <span class="text-success">Online</span>
                                                <span class="text-secondary">Offline</span>
                                        <td>{{ \Carbon\Carbon::parse($user->last_seen)->diffForHumans() }}</td>


Now login to your Laravel project to see the output like this:

The tutorial is over. You can download this project from GitHub. Thank you. 🙂


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


6 Replies to “How to Determine User Online Status & Last Seen…”

  1. I managed to fix it with this code

    @if ($user->last_seen ? \Carbon\Carbon::parse($user->last_seen)->toDateString() === date('Y-m-d') : null)
    {{ \Carbon\Carbon::parse($user->last_seen)->diffForHumans() }}
    @elseif ($user->last_seen ? \Carbon\Carbon::parse($user->last_seen)->toDateString() === date('Y-m-d', strtotime('-1 day')) : null)

    I would like to ask about the status, this solution says someone is offline when they are actually online, how can. I get this working

Leave a Reply

Your email address will not be published. Required fields are marked *