Laravel 9 User Login, Online Status & Last Seen

Laravel 9 check user login, online status and last seen; Through this tutorial, i am going to show you how to check user login, online status and last seen.

Laravel 9 User Login, Online Status & Last Seen

Follow the below given steps to implement how to check user login, online status and last seen in laravel 9 apps:

  • Step 1: Install Laravel 9 App
  • Step 2: Configure App to Database
  • Step 3: Generate Auth Scaffolding
  • Step 4: Add Column in User Table
  • Step 5: Create a Middleware
  • Step 6: Register Middleware in Kernel
  • Step 7: Create Controller by Artisan
  • Step 8: Check Online Status in Blade File
  • Step 9: Run Development Server

Step 1: Install Laravel 9 App

Run following command on command prompt to install laravel latest application:

 composer create-project --prefer-dist laravel/laravel blog 

Step 2: Configure App to Database

Go to your laravel app root directory and open .env file. Then add database details as follow:

 DB_CONNECTION=mysql 
 DB_HOST=127.0.0.1 
 DB_PORT=3306 
 DB_DATABASE=here your database name here
 DB_USERNAME=here database username here
 DB_PASSWORD=here database password here

Step 3: Generate Auth Scaffolding

Run the following commands on command prompt to auth scaffolding in laravel app:

# install laravel ui
composer require laravel/ui --dev
# auth scaffolding
php artisan ui vue --auth
# finally run
npm i && npm run dev

Step 4: Add Column in User Table

To add one column in the users table called last_seen.

So, navigate database>migrations folder and open create_users_table file. Then update the last_seen column in this file as follow:

public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->timestamp('email_verified_at')->nullable();
        $table->string('password');
        $table->timestamp('last_seen')->nullable();
        $table->rememberToken();
        $table->timestamps();
    });
}

Now migrate the migrations, So Execute the following command on command prompt:

php artisan migrate

Step 5: Create a Middleware

To create a middleware named ActivityByUser. So open your command prompt again and run the following command:

php artisan make:middleware ActivityByUser

Go to app>Http>Middleware and open ActivityByUser.php middleware. Then update the following code into your middleware file:

<?php
namespace App\Http\Middleware;
use App\Models\User;
use Closure;
use Auth;
use Cache;
use Carbon\Carbon;
class ActivityByUser
{
    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);
    }
}

Step 6: Register Middleware in Kernel

Go to app>Http and open Kernel.php. And register ActivityByUser.php middleware in the kernal.php file. You can see as follow:

protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\EncryptCookies::class,
        \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
        \Illuminate\Session\Middleware\StartSession::class,
        // \Illuminate\Session\Middleware\AuthenticateSession::class,
        \Illuminate\View\Middleware\ShareErrorsFromSession::class,
        \App\Http\Middleware\VerifyCsrfToken::class,
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
        \App\Http\Middleware\ActivityByUser::class,
    ],
    'api' => [
        'throttle:60,1',
        \Illuminate\Routing\Middleware\SubstituteBindings::class,
    ],
];

Step 7: Create Controller by Artisan

Run the following command on command prompt to create a controller named UserController:

php artisan make:controller UserController

Go to app>Http>Controllers and open UserController.php file. Then update the following method into your UserController.php file:

<?php
namespace App\Http\Controllers;
use App\Models\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>";
            else
                echo $user->name . " is offline. Last seen: " . Carbon::parse($user->last_seen)->diffForHumans() . " <br>";
        }
    }
}

Open routes>web.php and create a route:web.php

use App\Http\Controllers\UserController;
Route::get('status', [UserController::class, 'userOnlineStatus']);

Step 8: Check Online Status in Blade File

Now, You can also easily show online status in the blade file by using the following code:

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

Navigate to resources>views and open home.blade.php. Then update the following code into home.blade.php.

@extends('layouts.app')
@section('content')
    <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">
                                <thead>
                                <tr>
                                    <th>Name</th>
                                    <th>Email</th>
                                    <th>Status</th>
                                    <th>Last Seen</th>
                                </tr>
                                </thead>
                                <tbody>
                                @foreach($users as $user)
                                    <tr>
                                        <td>{{$user->name}}</td>
                                        <td>{{$user->email}}</td>
                                        <td>
                                            @if(Cache::has('user-is-online-' . $user->id))
                                                <span class="text-success">Online</span>
                                            @else
                                                <span class="text-secondary">Offline</span>
                                            @endif
                                        </td>
                                        <td>{{ \Carbon\Carbon::parse($user->last_seen)->diffForHumans() }}</td>
                                    </tr>
                                @endforeach
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection

The above code will show online or offline status and as well as last seen of all users on your blade view file in laravel.

Step 9: Run Development Server

Run php artisan serve command on command prompt to start your server locally:

php artisan serve

Conclusion

In this tutorial, you have learned how to show the online or offline status of users. And as well as last seen of users in laravel apps.

Recommended Laravel Tutorials

Be First to Comment

Leave a Reply

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