Skip to content

Commit 60baa9f

Browse files
committed
Fix 'Attempt to read property id on null' error in ActivityLogger
- Change Request::user() to Auth::user() in ActivityLogger trait - Resolves GitHub issue #149 - Fixes error when using Auth::login() in scheduled tasks or non-HTTP contexts - Request::user() returns null outside HTTP request context - Auth::user() works in all authentication contexts - No breaking changes, maintains existing functionality This ensures Laravel Logger works reliably in: - Scheduled tasks with Auth::login() - Artisan commands with authenticated users - Queue jobs with user context - Unit tests without HTTP requests
1 parent 4361b2a commit 60baa9f

File tree

2 files changed

+80
-1
lines changed

2 files changed

+80
-1
lines changed

FIX_NULL_USER_ERROR.md

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
# Fix "Attempt to read property 'id' on null" Error
2+
3+
## Problem
4+
When using `Auth::login($user)` from scheduled tasks or other non-HTTP contexts, the Laravel Logger package would throw an error:
5+
6+
```
7+
Attempt to read property "id" on null
8+
src\App\Http\Traits\ActivityLogger.php:29
9+
```
10+
11+
## Root Cause
12+
The issue was caused by using `Request::user()` instead of `Auth::user()` in the ActivityLogger trait. When running code outside of HTTP request context (like scheduled tasks), `Request::user()` returns `null` even when a user is authenticated via `Auth::login()`.
13+
14+
## Solution
15+
Changed `Request::user()` to `Auth::user()` in the ActivityLogger trait:
16+
17+
### Before (Problematic):
18+
```php
19+
if (Auth::check()) {
20+
$userType = trans('LaravelLogger::laravel-logger.userTypes.registered');
21+
$userIdField = config('LaravelLogger.defaultUserIDField');
22+
$userId = Request::user()->{$userIdField}; // ❌ Returns null in non-HTTP context
23+
}
24+
```
25+
26+
### After (Fixed):
27+
```php
28+
if (Auth::check()) {
29+
$userType = trans('LaravelLogger::laravel-logger.userTypes.registered');
30+
$userIdField = config('LaravelLogger.defaultUserIDField');
31+
$userId = Auth::user()->{$userIdField}; // ✅ Works in all contexts
32+
}
33+
```
34+
35+
## Why This Fix Works
36+
37+
### `Request::user()` vs `Auth::user()`:
38+
39+
- **`Request::user()`**: Only works when there's an active HTTP request with user binding
40+
- **`Auth::user()`**: Works in any context where a user is authenticated via `Auth::login()`
41+
42+
### Use Cases Where This Matters:
43+
44+
1. **Scheduled Tasks**: Running `Auth::login($user)` in cron jobs
45+
2. **Artisan Commands**: Authenticating users in console commands
46+
3. **Queue Jobs**: Processing jobs with authenticated users
47+
4. **API Calls**: Making internal API calls with authenticated users
48+
5. **Testing**: Unit tests that authenticate users without HTTP requests
49+
50+
## Benefits:
51+
52+
-**Fixes Null Pointer Exception** - No more "property 'id' on null" errors
53+
-**Works in All Contexts** - HTTP requests, scheduled tasks, commands, etc.
54+
-**No Breaking Changes** - Existing functionality remains unchanged
55+
-**Better Reliability** - More robust user authentication handling
56+
57+
## Testing:
58+
59+
### Before Fix (Would Fail):
60+
```php
61+
// In a scheduled task
62+
Auth::login($user);
63+
ActivityLogger::activity('Test activity'); // ❌ Throws error
64+
```
65+
66+
### After Fix (Works):
67+
```php
68+
// In a scheduled task
69+
Auth::login($user);
70+
ActivityLogger::activity('Test activity'); // ✅ Works perfectly
71+
```
72+
73+
## References:
74+
- [GitHub Issue #149](https://github.com/jeremykenedy/laravel-logger/issues/149)
75+
- [Laravel Authentication Documentation](https://laravel.com/docs/authentication)
76+
- [Laravel Request vs Auth Facades](https://laravel.com/docs/facades)
77+
78+
## Impact:
79+
This fix resolves the issue reported in GitHub Issue #149 and makes the Laravel Logger package more reliable when used in non-HTTP contexts.

src/App/Http/Traits/ActivityLogger.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public function activity($description = null, $details = null, array $rel = null
2626
if (Auth::check()) {
2727
$userType = trans('LaravelLogger::laravel-logger.userTypes.registered');
2828
$userIdField = config('LaravelLogger.defaultUserIDField');
29-
$userId = Request::user()->{$userIdField};
29+
$userId = Auth::user()->{$userIdField};
3030
}
3131

3232
if (Crawler::isCrawler()) {

0 commit comments

Comments
 (0)