weblynx/laravel-model-audit

Audit package for laravel models

dev-master 2022-03-13 16:03 UTC

This package is auto-updated.

Last update: 2025-06-13 23:17:27 UTC


README

Warning: this is still in beta. So you may encounter problems.

This is just a side project soo updates may come rare.

Installation

composer require weblynx/laravel-model-audit

After installing run

php artisan migrate

Lifecycle events available:

  • created
  • updated
  • deleted
  • restored
  • soft-deleted
  • force-deleted

Publish vendor file

php artisan vendor:publish

Inside of config you will find an auditor.php file.

You can disable any of events globally by commenting the line.

<?php

return [
    'options' => [
        'created',
        'updated',
//        'deleted',
        'restored',
        'soft-deleted',
        'force-deleted'
    ]
];

You can disable any of events by adding $excludedAudits attribute on model

    protected array $excludedAudits = [
        'created'
    ];

Traits

WithAuditor

WithAuditor is used to activate th events of the package. Use it on model you want to be audited.

class Model {
    use WithAuditor;
}

HasManyAudits

HasManyAudits returns a hasMany relationship on the model that has created models.

class Model {
    use HasManyAudits;
}

Model

class Auditor

Auditor class has a dynamic belongsToRelationship based on the provider defined in config. Ex:

Will return a belongsTo relationship with the name: user().

 'providers' => [
        'users' => [
            'driver' => 'eloquent',
            'model' => App\Models\User::class,
        ],
    ],

Next steps:

  • Add fields that can be excluded from model.
  • Add unit testing.