Tracking login history records, IP addresses, devices and timestamps. Over 70% of apps use this data for security monitoring and user behaviour insights.
Key Points
When you invest in custom web app development, security and user tracking become crucial. A reliable web development company often recommends logging user activities, especially logins, to enhance security, auditing and user behaviour analytics.
In this guide, you’ll learn how to monitor login history and IP addresses in Laravel. This is a useful feature you can add to any application, whether you’re offering website development services for clients or building your own SaaS platform.
We’ll cover how to capture:
Let’s get started!
Businesses that invest in custom development often prioritize security and accountability. Tracking user logins allows you to:
Whether you’re managing your own SaaS or delivering website development services for clients, having this feature built into your web app makes your system more secure and transparent.
To store login data, we first need to create a dedicated table. Run the following command to generate a model and migration:
php artisan make:model LoginHistory -m
Open the generated migration file and edit it like this:
Run the migration to create the table: php artisan migrate
Storing login histories is important for applications developed through custom projects, as it helps you monitor who’s accessing your system and when.
Next, define the model so Laravel knows how to interact with your new table.
Open:
app/Models/LoginHistory.php
Add this:
A solid data model is the backbone of any solution from a professional web development company. This model lets us quickly store and retrieve login records and link them to users.
Laravel automatically fires a Login event whenever a user logs in. We’ll capture this event and record the login details.
Generate a listener:
Edit your listener at:
app/Listeners/LogSuccessfulLogin.php
Add the following code:
Captures the logged-in user
Retrieves the user’s IP address and browser details
Stores this data in the database
Such event-driven solutions are a hallmark of advanced custom website development.
Laravel won’t know about your listener until you register it.
Open:
app/Providers/EventServiceProvider.php
Find the $listen array and add your listener:
Without registering, your listener won’t execute!
Let’s build a basic login form and logic.
Create:
app/Http/Controllers/LoginController.php
Add:
Create a simple login page:
resources/views/auth/login.blade.php
This simple form posts to your login route.
Admins should be able to see who logged in and when.
Create:
app/Http/Controllers/LoginHistoryController.php
Add:
Whether you’re running your app or offering website development services, reporting features like this are valuable additions.
Now, let’s display login records in a beautiful table.
resources/views/admin/login-history.blade.php
A professional web development company often integrates Bootstrap to ensure dashboards are mobile-friendly and elegant.
Finally, register your routes in:
routes/web.php
This gives you:
When a user logs in:
Laravel fires the Login event.
Your listener automatically records login details.
A record is saved in the login_histories table, including:
Admins can view all login history via:
/admin/login-history
User |
IP Address |
User Agent |
Logged In At |
John Doe |
192.168.1.10 |
Mozilla/5.0 (Windows NT 10.0; Win64; x64) |
04 Jul 2025, 09:35 AM |
Tracking login history and IP addresses is a crucial feature for modern web applications. It improves security, auditing, and user insights. If you’re delivering website development services, it’s a great value-add. If you’re building your app, it’s a solid investment in security.
Need professional help? Consider hiring Laravel developer talent who understands Laravel’s event system and security best practices. This solution is lightweight, efficient, and perfect for any custom web app development project.
©2025Digittrix Infotech Private Limited , All rights reserved.