Mrunali Khandekar
Mrunali Khandekar

Mrunali Khandekar

How to display online users in laravel.

Mrunali Khandekar's photo
Mrunali Khandekar
·Dec 29, 2020·

3 min read

Subscribe to my newsletter and never miss my upcoming articles

Now we will list the user that are online using Laravel. Follow the below steps.

1. Install Laravel Project. Type the below code in your terminal.

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

After installation you can see something like below image. install 2.PNG

2. Now Install Auth. To Install Auth type below command in your terminal.

composer require laravel/ui
php artisan ui vue --auth
npm install
npm run dev

3. Create Middleware. To create middleware type below command in terminal.

php artisan make:middleware OnlineStatus

4. Add the below code in Middleware OnlineStatus in app/Http/Middleware/OnlineStatus.

  public function handle(Request $request, Closure $next)
            $expiresAt = Carbon::now()->addMinutes(5);
        return $next($request);

5. Go to Kernel.php . You will find kernel.php in app/Http/Kernel.php*. Write the below line in web array.

 'web' => [

6. In User Model add the below function in app/Models/User.php.

 public function isOnline(){
        return Cache::has('user-online-'.$this->id);

7. Define route.Add the below code routes/web.php.


8. Create a controller. To create controller type below command in your terminal.

php artisan make:controller userController

9. Add below code in userController in app/Http/Controllers/userController.php.

public function index(){
$users = User::all();
return view('home1',compact('users'));

Note: Create a blade file in resources/views and name it home1.blade.php.

10. Go to home1.blade.php. In resources/views/home.blade.php.

  <table class="table">
                        <th>Account Created At</th>
                    @foreach($users as $user)


*Register users and keep login a user you will the name of the user who is online on your website.

Now Open your browser and enter link in my case it /active-status.*

Did you find this article valuable?

Support Mrunali Khandekar by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
Share this