Available for select projects New York · Working worldwide
Rappasoft LLC
Menu

Laravel Authentication Log / Documentation / v6

Statistics and Insights

Installation, configuration, and usage guidance maintained with the project.

The package provides comprehensive statistics and insights about user authentication patterns.

Getting Statistics

Get all statistics for a user:

1$user = User::find(1);
2$stats = $user->getLoginStats();
3 
4// Returns:
5// [
6// 'total_logins' => 150,
7// 'failed_attempts' => 5,
8// 'unique_devices' => 3,
9// 'unique_ips' => 8,
10// 'last_30_days' => 45,
11// 'last_7_days' => 12,
12// 'suspicious_activities' => 2,
13// 'trusted_devices' => 2,
14// ]

Individual Statistics

Get specific statistics:

1$user = User::find(1);
2 
3// Total successful logins
4$totalLogins = $user->getTotalLogins();
5 
6// Total failed login attempts
7$failedAttempts = $user->getFailedAttempts();
8 
9// Number of unique devices
10$uniqueDevices = $user->getUniqueDevicesCount();
11 
12// Number of suspicious activities
13$suspiciousCount = $user->getSuspiciousActivitiesCount();

Example: Statistics Dashboard

Here's an example of displaying statistics in a dashboard:

1// In your controller
2public function dashboard()
3{
4 $user = auth()->user();
5 $stats = $user->getLoginStats();
6 
7 return view('dashboard', compact('stats'));
8}
9 
10// In your view
11<div class="stats-grid">
12 <div class="stat-card">
13 <h3>Total Logins</h3>
14 <p class="stat-value">{{ $stats['total_logins'] }}</p>
15 </div>
16 
17 <div class="stat-card">
18 <h3>Failed Attempts</h3>
19 <p class="stat-value">{{ $stats['failed_attempts'] }}</p>
20 </div>
21 
22 <div class="stat-card">
23 <h3>Unique Devices</h3>
24 <p class="stat-value">{{ $stats['unique_devices'] }}</p>
25 </div>
26 
27 <div class="stat-card">
28 <h3>Last 30 Days</h3>
29 <p class="stat-value">{{ $stats['last_30_days'] }}</p>
30 </div>
31 
32 @if($stats['suspicious_activities'] > 0)
33 <div class="stat-card alert">
34 <h3>Suspicious Activities</h3>
35 <p class="stat-value">{{ $stats['suspicious_activities'] }}</p>
36 <a href="{{ route('security.review') }}">Review</a>
37 </div>
38 @endif
39</div>

Combining with Query Scopes

You can combine statistics with query scopes for more detailed insights:

1$user = User::find(1);
2 
3// Get statistics for last 7 days only
4$recentLogs = $user->authentications()->recent(7)->get();
5$recentStats = [
6 'total' => $recentLogs->where('login_successful', true)->count(),
7 'failed' => $recentLogs->where('login_successful', false)->count(),
8 'suspicious' => $recentLogs->where('is_suspicious', true)->count(),
9];
10 
11// Get statistics by device
12$deviceStats = $user->authentications()
13 ->successful()
14 ->select('device_id', 'device_name', \DB::raw('count(*) as login_count'))
15 ->groupBy('device_id', 'device_name')
16 ->get();