Table of Contents
Laravel is a popular PHP web application framework that provides developers with an easy-to-use and highly customizable toolset to create robust web applications. One of the many features Laravel provides is the ability to check the online status of a user in your application. In this article, we will guide you on how to detect user online or offline on laravel step-by-step through creating a middleware to update the last_seen field and adding a method to the User model to check the user’s online status.
Creating a Middleware to Update Last Seen Field
A middleware is a piece of code that sits between the request and the response in a Laravel application. It allows you to intercept incoming HTTP requests and modify them before they reach the controller or the database. In our case, we will create a middleware to update the last_seen field of a user every time they request the Laravel app.
Step 1: Create a Middleware
To create a middleware, you can use the following command in the terminal:
php artisan make:middleware UpdateLastSeenMiddleware
This command will create a new file in the app/Http/Middleware
directory with the name UpdateLastSeenMiddleware.php
.
Step 2: Implement the Middleware Logic
Now that we have created a new middleware, we can implement the logic to update the last_seen field of the user. The following code snippet shows how to do that:
In the above code snippet, we first check if the user is authenticated using the Auth::check()
method. If the user is authenticated, we retrieve the authenticated user using the Auth::user()
method. We then update the last_seen
field of the user using the Carbon::now()->addMinute(1)
method, which sets the value to the current timestamp plus one minute. Finally, we save the user object.
Step 3: Register the Middleware
The last step is to register the middleware in the app/Http/Kernel.php
file. Add the following line to the $routeMiddleware
array:
The update.lastseen
key is a custom name for the middleware, and you can use any name you like. Now, the middleware is registered, and we can use it to update the last_seen
field of the user.
Adding a Method to the User Model for isOnline() Functionality
Now that we have created a middleware to update the last_seen
field of the user, we can add a method to the User
model to check whether the user is online or offline.
Step 1: Add a Method to the User Model
To add a method to the User
model, open the app/Models/User.php
file and add the following code:
In the above code snippet, we define a new method called isOnline()
, which returns a boolean value indicating whether the user is online or not. We do this by checking the difference between the current
time and the last_seen
field of the user using the diffInMinutes()
method provided by the Carbon library. If the difference is less than one minute, we consider the user to be online, and the method returns true
. Otherwise, the method returns false
.
Step 2: Using the isOnline()
Method
Now that we have added the isOnline()
method to the User
model, we can use it in our application to check the online status of a user. For example, we can add the following code in our view to display a green dot if the user is online and a red dot if the user is offline:
In the above code snippet, we check if the isOnline()
method returns true
for the user, and we display a green dot with the dot-online
CSS class. Otherwise, we display a red dot with the dot-offline
CSS class.
While Ending
Now you can check user online or offline on Laravel. We have learned how to check the online status of a user in a Laravel application. We have created a middleware to update the last_seen
field of the user every time they make a request to the Laravel app, and we have added a method to the User
model to check whether the user is online or offline. By using these techniques, you can provide a better user experience in your Laravel application by displaying the online status of the users.
Remember that the isOnline()
method checks the last seen time and not the actual online status of the user. If the user closes the browser window without logging out, the isOnline()
method will still return true
until the last_seen
field is updated by the middleware. Therefore, you should use this method as an approximation of the online status of the user and not as a definitive indicator.
We hope this article has been helpful in your journey to building better Laravel applications. Happy coding!
Add comment