|
| 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. |
0 commit comments