Skip to content

Commit

Permalink
Update RecordActivity.php
Browse files Browse the repository at this point in the history
  • Loading branch information
amdad121 committed Dec 31, 2023
1 parent 82d8c94 commit f32f031
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions src/RecordActivity.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,30 @@

namespace AmdadulHaq\RecordActivity;

use Illuminate\Support\Facades\Auth;

trait RecordActivity
{
public static function bootRecordActivity()
public static function bootRecordActivity(): void
{
// updating created_by and updated_by when model is created
static::creating(function ($model) {
$user = Auth::user();
if ($user) {
$model->created_by = $user->id;
$model->save();
if (! $model->isDirty('created_by')) {
$model->created_by = auth()->user()->id;
}

if (! $model->isDirty('updated_by')) {
$model->updated_by = auth()->user()->id;
}
});

static::updating(function ($model) {
$user = Auth::user();
if ($user) {
$model->updated_by = $user->id;
$model->save();
if (! $model->isDirty('updated_by')) {
$model->updated_by = auth()->user()->id;
}
});

static::deleting(function ($model) {
$user = Auth::user();
if ($user) {
$model->deleted_by = $user->id;
$model->save();
if (! $model->isDirty('deleted_by')) {
$model->deleted_by = auth()->user()->id;
}
});
}
Expand Down

0 comments on commit f32f031

Please sign in to comment.