Skip to content

Instantly share code, notes, and snippets.

@aytacmalkoc
Last active August 4, 2024 12:54
Show Gist options
  • Save aytacmalkoc/2ec63805d6311ddd2228e7d6cd42104b to your computer and use it in GitHub Desktop.
Save aytacmalkoc/2ec63805d6311ddd2228e7d6cd42104b to your computer and use it in GitHub Desktop.

Revisions

  1. aytacmalkoc revised this gist Aug 4, 2024. 1 changed file with 22 additions and 3 deletions.
    25 changes: 22 additions & 3 deletions HasCrudLogs.php
    Original file line number Diff line number Diff line change
    @@ -30,10 +30,29 @@ protected static function logAction($action, $model, $isDeleted = false): void
    $userId = auth()->id() ?? 'guest';
    $className = get_class($model);
    $modelId = $model->getKey();
    $changes = $isDeleted ? $model->getAttributes() : $model->getDirty();
    $logLevel = self::logType($action);

    Log::channel('hub')->$logLevel("User {$userId} is {$action} a {$className} with ID {$modelId}", ['changes' => $changes]);
    if ($action === 'updated') {
    $original = $model->getOriginal();
    $changes = $model->getDirty();

    Log::channel('hub')->$logLevel("User {$userId} has {$action} a {$className} with ID {$modelId}", [
    'original' => $original,
    'changes' => $changes
    ]);
    } elseif ($isDeleted) {
    $changes = $model->getAttributes();

    Log::channel('hub')->$logLevel("User {$userId} has {$action} a {$className} with ID {$modelId}", [
    'attributes' => $changes
    ]);
    } else {
    $changes = $model->getDirty();

    Log::channel('hub')->$logLevel("User {$userId} has {$action} a {$className} with ID {$modelId}", [
    'changes' => $changes
    ]);
    }
    }

    protected static function logType(string $action): string
    @@ -45,4 +64,4 @@ protected static function logType(string $action): string
    default => 'debug',
    };
    }
    }
    }
  2. aytacmalkoc created this gist Aug 4, 2024.
    48 changes: 48 additions & 0 deletions HasCrudLogs.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    <?php

    namespace App\Traits;

    use Illuminate\Support\Facades\Log;

    trait HasCrudLogs
    {
    public static function bootHasCrudLogs(): void
    {
    static::created(function ($model) {
    self::logAction('created', $model);
    });

    static::updated(function ($model) {
    self::logAction('updated', $model);
    });

    static::deleted(function ($model) {
    self::logAction('deleted', $model, true);
    });

    static::replicating(function ($model) {
    self::logAction('replicating', $model);
    });
    }

    protected static function logAction($action, $model, $isDeleted = false): void
    {
    $userId = auth()->id() ?? 'guest';
    $className = get_class($model);
    $modelId = $model->getKey();
    $changes = $isDeleted ? $model->getAttributes() : $model->getDirty();
    $logLevel = self::logType($action);

    Log::channel('hub')->$logLevel("User {$userId} is {$action} a {$className} with ID {$modelId}", ['changes' => $changes]);
    }

    protected static function logType(string $action): string
    {
    return match ($action) {
    'creating', 'created' => 'info',
    'updating', 'updated' => 'notice',
    'deleting', 'deleted' => 'warning',
    default => 'debug',
    };
    }
    }