phpicoder Dec 17, 2021 laravel

Hi guys, In this tutorial we will learn How to check user is online or not in laravel web application. we will make example and this example we show user list and show user online or offline.

Many applications require user status. As the admin can check every time who is currently using my app.

So you need user status. IF yes you are right please, now we make step by step how to get user status.

Step 1 : Create a Middleware

We need middleware name userActivityNow we will create middleware from the following command.

php artisan make:middleware userActivityNow

After run above command we go to app/Http/Middleware path and then we have a one file name userActivityNow.php and then open this file and edit following code.

<?php

namespace App\Http\Middleware;

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

class userActivityNow
{
    /**
     * 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 2 : Add Middleware to Kernel

Now go to app/Http path and open Kernel.php file and add UserActivityNow middleware class under middlewareGroups using web arrays.

So follow the code below

protected $middlewareGroups = [
    'web' => [

        \App\Http\Middleware\UserActivityNow::class,

    ],
];
Step 3 Creaet Route

Open routes/web.php and add following route 

Route::get('/users', 'UserController@userOnlineStatus');
Step 4 Creaet Controller´╗┐

Now go to app/Http/Controllers path and create one file name UserController.php

open UserController.php and edit following code.

<?php

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 $key => $user) {
            if (Cache::has('user-is-online-' . $user->id)){
                $users[$key]->status = 'Online';
            }else{
                $users[$key]->status = 'Offline';
            }
        }

        return view('users.index',compact('users'))->with('i',0);
    }
}
Step 5 Creaet View File

go to resources/views path under create users folder and this user folder ccreate index.blade.php file 

opne index.blade.php this file and edit following code.

@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">
                        <div class="container">
                            <table class="table table-bordered">
                                <thead>
	                                <tr>
	                                    <th>Name</th>
	                                    <th>Email</th>
	                                    <th>Status</th>
	                                </tr>
                                </thead>
                                <tbody>
                                    @if(!empty($users) && $users->count())
                                        @foreach($users as $key => $value)
                                            <tr>
                                                <td>{{$value->name}}</td>
                                                <td>{{$value->email}}</td>
                                                <td>{{$value->status}}</td>
                                            </tr>
                                        @endforeach
                                    @endif
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection

Demo is ready, you are ready, let's go run the code following command

php artisan serve

Now you can open following URL on your browser: http://localhost:8000/users

I hope this tutorial help for you.