From 6c198f2f5642bdde24f163117d019fd27b59ed1c Mon Sep 17 00:00:00 2001 From: Dipesh Khanal Date: Sun, 21 Sep 2025 10:51:41 +0545 Subject: [PATCH] ref: check auth after event --- src/Traits/HasLog.php | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/Traits/HasLog.php b/src/Traits/HasLog.php index f184bf1..6b92017 100644 --- a/src/Traits/HasLog.php +++ b/src/Traits/HasLog.php @@ -26,22 +26,22 @@ public static function boot() /** * The created event for the model is listened to and the logs are created. */ - if (auth()->user()) { - static::created(function ($model) { + static::created(function ($model) { + if (auth()->check()) { $model->logs()->create([ 'action' => 'Create', 'ip_address' => request()->ip(), 'device' => request()->userAgent(), 'user_id' => auth()->user()->id ]); - }); - } + } + }); /** * The updated event for the model is listened to and the logs are created. */ - if (auth()->user()) { - static::updated(function ($model) { + static::updated(function ($model) { + if (auth()->check()) { $model->logs()->create([ 'action' => 'Update', 'ip_address' => request()->ip(), @@ -50,22 +50,23 @@ public static function boot() 'old_data' => json_encode($model->getRawOriginal()), 'changed_values' => json_encode($model->getChanges()) ]); - }); - } + } + }); /** * The deleted event for the model is listened to and the logs are created. */ - if (auth()->user()) { - static::deleted(function ($model) { + static::deleted(function ($model) { + if (auth()->check()) { $model->logs()->create([ 'action' => 'Delete', 'ip_address' => request()->ip(), 'device' => request()->userAgent(), 'user_id' => auth()->user()->id ]); - }); - } + } + }); + }