rrvwmrrr/little-auditor

This package is abandoned and no longer maintained. No replacement package was suggested.

Mimimal auditing package for Laravel

dev-main 2021-05-16 08:36 UTC

This package is auto-updated.

Last update: 2021-06-16 08:55:17 UTC


README

Little Auditor - The littlest Laravel auditor

Little Auditor

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Little auditor contains a pair of traits you can apply to your Eloquent models to allow you to hook in to their events and store the attached data.

Should I use this package?

No. It's not ready yet.

Who should consider this package?

There are no extra functionalities here, we're just keeping track of a model, changes that have been made to it and the user that made them.

If you need fully fledged auditing functionality, I'd recommend Laravel Auditing.

Auditing

If you've turned off package discovery, you'll need to register the Audit service provider in your app config.

    'providers' => [
        ...
        Rrvwmrrr\Auditor\AuditServiceProvider::class,
        ...

    ]

Setting up an auditor

The auditor will always attempt to grab the logged in user id and attach that to an audit. The default auditor is mapped to App\Models\User but can be changed via a service provivider if you're authenticating with a different model.

// app/Providers/AppServiceProvider.php

use Rrvwmrrr\Auditor\Auditor;

public function boot()
{
    Auditor::$auditorModel = Some\Other\Class::class;
}

Now that we've configured the model for our Auditor, let's set up the trait on that model.

// app/Models/User.php

use Rrvwmrrr\Auditor\Traits\IsAuditor;

class User extends Authenticatable
{
    use IsAuditor;

Setting up auditable models

Finally, we need to apply the auditing trait for the models we want to track changes on.

// app/Models/Order.php

use Rrvwmrrr\Auditor\Traits\IsAudited;

class Order
{
    use IsAudited;
}

Defaults

By default, the IsAudited trait will try to listen for all model events. This can be customized via each model using the audit property

// app/Models/Order.php

use Rrvwmrrr\Auditor\Traits\IsAudited;

class Order
{
    use IsAudited;

    /**
     * The model events to listen for
     *
     * @var array
     */
    protected $audit = [
        'saved',
    ];
}

This set up would only save audit information when the saved event is fired for the model.

Audit relationships

Audits can now be queried on both the user model (retreiving everything they've audited) and any auditable model.

    $user = User::find(1);
    $user->audits;   //  Collection of audits made by the user

    $order = Order::find(1);
    $order->audits; //  Collection of audits made on the order

Testing

composer test

Contributors

License

The MIT License (MIT). Please see License File for more information.