How to Check User Online or Not in Laravel 7

In this tutorial you will learn about the How to Check User Online or Not in Laravel 7 and its application with practical example.

In this How to Check User Online or Not in Laravel 7 I’ll show you how to check User Online or Not in Laravel 7 using custom middleware. In this tutorial you will learn to check user is online or not in laravel. In this step by step guide we will be creating a custom middleware to check User Online or Not.

How to Check User Online or Not in Laravel 7

  • Step 1: Install New Laravel App
  • Step 2: Add Database Detail
  • 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 New Laravel App

First of all we need to create a fresh laravel project, download and install Laravel using the below command

Step 2: Add Database Details

Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.

Step 3: Generate Auth Scaffolding

In this step we will be generating laravel’s auth scaffolding using following command:

Step 4: Add Column in User Table

You 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:

Now, run the migration to create database table using following artisan command:

Step 5: Create a Middleware

In this step we will creating custom middleware using following command:

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

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. You can see as follow:

Step 7: Create Controller by Artisan

Now, lets create a controller named UserController using command given below –

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

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

Step 8: Check Online Status in Blade File

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

Step 9: Run Development Server

Now we are ready to run our example so lets start the development server using following artisan command –

In this tutorial we have learn about the How to Check User Online or Not in Laravel 7 and its application with practical example. I hope you will like this tutorial.