Laravel-auditing: No changes to model data but audit log entry is created

Created on 29 Mar 2017  路  7Comments  路  Source: owen-it/laravel-auditing

When a model is saved but no changes in data, and entry in the audits table is created, with the old_values and new_values being just []

Is this behavior intended?

And if so, is there a way to make it log only when there are changes?

Thanks!

2017-03-29 15 48 36

invalid wontfix

Most helpful comment

Just in case anyone else has the same problem, here's what I did.

Create a new driver using the console command: ./artisan make:audit-driver NoEmptyAudits

This will create a new file: app/AuditDrivers/NoEmptyAudits.php

The name of the class in this new file may remain as CustomDriver so you will need to rename it to NoEmptyAudits. Not sure if this is a bug or intended behavior.

In audit(), add the lines:

    $audit = $model->toAudit();

    if (empty($audit['old_values']) and empty($audit['new_values'])) {
        return null;
    }

    return Audit::create($audit);

As for prune(), you can just copy the same code inside vendor/owen-it/laravel-auditing/src/Drivers/Database.php

Then to make laravel-auditing use your new driver, in config/audit.php

'default' => 'App\AuditDrivers\NoEmptyAudits',

'drivers' => [
    'database' => [
        'table'      => 'audits',
        'connection' => null,
    ],
    'App\AuditDrivers\NoEmptyAudits' => [
        'table'      => 'audits',
        'connection' => null,
    ],
],

All 7 comments

Hi @wyred , this does not look like the default behavior. You can provide an example code?

Hi @anteriovieira ,

The code I use to trigger the above is just:

$content = \App\Content::find(1);
$content->save();

I tried it again but with a different model and it didn't happen. The only difference between these two models is that the one with the problem is using https://github.com/dimsav/laravel-translatable .

Edit: I'm using v5.6 of laravel-translatable. I'll update to the latest version (v7) tomorrow and I'll post an update here if it fixes the problem.

Ping @quetzyg

Updating laravel-translatable to v7 does not fix the problem.

Hi @wyred,

This is an issue with the dimsav/laravel-translatable package, more specifically in the Translatable.php trait.

The save() method event fire logic does not make sense:

            if (count($this->getDirty()) > 0) {
                // If $this->exists and dirty, parent::save() has to return true. If not,
                // an error has occurred. Therefore we shouldn't save the translations.
                if (parent::save($options)) {
                    return $this->saveTranslations();
                }
                return false;
            } else {
                // If $this->exists and not dirty, parent::save() skips saving and returns
                // false. So we have to save the translations
                if ($saved = $this->saveTranslations()) {
                    $this->fireModelEvent('saved', false);
                    $this->fireModelEvent('updated', false);
                }
                return $saved;
            }

As you can see from the code fragment, the updated event is fired when no changes were made to the main model. Raise an issue with the package maintainer so he can apply the fix where appropriate.

I'm marking this issue as closed now.

@quetzyg Ok, thanks!

Just in case anyone else has the same problem, here's what I did.

Create a new driver using the console command: ./artisan make:audit-driver NoEmptyAudits

This will create a new file: app/AuditDrivers/NoEmptyAudits.php

The name of the class in this new file may remain as CustomDriver so you will need to rename it to NoEmptyAudits. Not sure if this is a bug or intended behavior.

In audit(), add the lines:

    $audit = $model->toAudit();

    if (empty($audit['old_values']) and empty($audit['new_values'])) {
        return null;
    }

    return Audit::create($audit);

As for prune(), you can just copy the same code inside vendor/owen-it/laravel-auditing/src/Drivers/Database.php

Then to make laravel-auditing use your new driver, in config/audit.php

'default' => 'App\AuditDrivers\NoEmptyAudits',

'drivers' => [
    'database' => [
        'table'      => 'audits',
        'connection' => null,
    ],
    'App\AuditDrivers\NoEmptyAudits' => [
        'table'      => 'audits',
        'connection' => null,
    ],
],
Was this page helpful?
0 / 5 - 0 ratings