Skip to content

Commit 8f94ffc

Browse files
authored
Merge pull request #7 from dipesh79/log
refactor: check auth after event
2 parents bf3d228 + 6c198f2 commit 8f94ffc

1 file changed

Lines changed: 13 additions & 12 deletions

File tree

src/Traits/HasLog.php

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,22 +26,22 @@ public static function boot()
2626
/**
2727
* The created event for the model is listened to and the logs are created.
2828
*/
29-
if (auth()->user()) {
30-
static::created(function ($model) {
29+
static::created(function ($model) {
30+
if (auth()->check()) {
3131
$model->logs()->create([
3232
'action' => 'Create',
3333
'ip_address' => request()->ip(),
3434
'device' => request()->userAgent(),
3535
'user_id' => auth()->user()->id
3636
]);
37-
});
38-
}
37+
}
38+
});
3939

4040
/**
4141
* The updated event for the model is listened to and the logs are created.
4242
*/
43-
if (auth()->user()) {
44-
static::updated(function ($model) {
43+
static::updated(function ($model) {
44+
if (auth()->check()) {
4545
$model->logs()->create([
4646
'action' => 'Update',
4747
'ip_address' => request()->ip(),
@@ -50,22 +50,23 @@ public static function boot()
5050
'old_data' => json_encode($model->getRawOriginal()),
5151
'changed_values' => json_encode($model->getChanges())
5252
]);
53-
});
54-
}
53+
}
54+
});
5555

5656
/**
5757
* The deleted event for the model is listened to and the logs are created.
5858
*/
59-
if (auth()->user()) {
60-
static::deleted(function ($model) {
59+
static::deleted(function ($model) {
60+
if (auth()->check()) {
6161
$model->logs()->create([
6262
'action' => 'Delete',
6363
'ip_address' => request()->ip(),
6464
'device' => request()->userAgent(),
6565
'user_id' => auth()->user()->id
6666
]);
67-
});
68-
}
67+
}
68+
});
69+
6970

7071
}
7172

0 commit comments

Comments
 (0)