Anvil
Anvil - The mobile companion for Laravel Forge. Available now. Download for iOS

Getting Logs

Basic Log Retrieval

Get all authentication logs for the user:

1User::find(1)->authentications;

Get the latest authentication:

1User::find(1)->latestAuthentication;

Login Information Methods

Get the user's last login information:

1User::find(1)->lastLoginAt();
2 
3User::find(1)->lastSuccessfulLoginAt();
4 
5User::find(1)->lastLoginIp();
6 
7User::find(1)->lastSuccessfulLoginIp();

Get the user's previous login time & IP address (ignoring the current login):

1auth()->user()->previousLoginAt();
2 
3auth()->user()->previousLoginIp();

Query Scopes

The AuthenticationLog model provides powerful query scopes for filtering:

1use Rappasoft\LaravelAuthenticationLog\Models\AuthenticationLog;
2 
3// Filter successful logins
4$successfulLogins = AuthenticationLog::successful()->get();
5 
6// Filter failed logins
7$failedLogins = AuthenticationLog::failed()->get();
8 
9// Filter by IP address
10$ipLogs = AuthenticationLog::fromIp('192.168.1.1')->get();
11 
12// Filter recent logs (last 7 days by default)
13$recentLogs = AuthenticationLog::recent(7)->get();
14 
15// Filter suspicious activities
16$suspicious = AuthenticationLog::suspicious()->get();
17 
18// Filter active sessions
19$activeSessions = AuthenticationLog::active()->get();
20 
21// Filter trusted devices
22$trustedDevices = AuthenticationLog::trusted()->get();
23 
24// Filter by device ID
25$deviceLogs = AuthenticationLog::fromDevice($deviceId)->get();
26 
27// Filter for specific user
28$userLogs = AuthenticationLog::forUser($user)->get();
29 
30// Chain multiple scopes
31$recentSuspicious = AuthenticationLog::recent(30)->suspicious()->get();

Statistics

Get comprehensive login statistics:

1$user = User::find(1);
2 
3// Get all statistics
4$stats = $user->getLoginStats();
5// Returns:
6// [
7// 'total_logins' => 150,
8// 'failed_attempts' => 5,
9// 'unique_devices' => 3,
10// 'unique_ips' => 8,
11// 'last_30_days' => 45,
12// 'last_7_days' => 12,
13// 'suspicious_activities' => 2,
14// 'trusted_devices' => 2,
15// ]
16 
17// Or get individual stats
18$totalLogins = $user->getTotalLogins();
19$failedAttempts = $user->getFailedAttempts();
20$uniqueDevices = $user->getUniqueDevicesCount();
21$suspiciousCount = $user->getSuspiciousActivitiesCount();