Laravel 9 Check User Login, Online Status & Last Seen

Laravel 9 Check User Login, Online Status & Last Seen

Laravel 9 check user login, online status and last seen; In this tutorial, we will learn how to check user online status and last seen.

Laravel 9 Check User Login, Online Status & Last Seen

Use the following steps to implement how to check if user is logged in laravel 9 apps:

  • Step 1: Install Laravel 9 App
  • Step 2: Connecting 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

In this step, Execute the following command on the terminal to install laravel latest application setup, So open terminal OR command prompt and run the following command:

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

Step 2: Connecting App to Database

After successfully installing laravel new app. Next, Go to 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 database name here
 DB_USERNAME =  here database username here
 DB_PASSWORD = here database password here

Step 3: Generate Auth Scaffolding

In this step, Execute the following commands on terminal for auth scaffolding in laravel 9 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

In this step, need 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

In this step, Create a middleware named ActivityByUser. So open command prompt again and run the following command:

php artisan make:middleware ActivityByUser

Next, navigate to app>Http>Middleware and open ActivityByUser.php middleware. Then update the following code into 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

In this step, navigate app>Http and open Kernel.php. And register ActivityByUser.php middleware in the kernal.php file. 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

In this step, execute the following command on the terminal to create a controller named UserController. So run the following command on the command prompt:

php artisan make:controller UserController

Next, navigate to app>Http>Controllers and open UserController.php file. Then update the following method into 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

In this step, 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 blade view file in laravel.

Step 9: Run Development Server

In this step, use the following php artisan serve command to start server locally:

php artisan serve

Conclusion

In this tutorial, we 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

Recommended:-Laravel Try Catch

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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