owen-it/laravel-auditing

Duplicate entries for every action

K-Neu-Projects opened this issue · 8 comments

Q A
Bug? yes
New Feature? no
Framework Laravel
Framework version 8.0
Package version 13.6
PHP version 8.0

Actual Behaviour

The audits table creates two identical entries for every action.

Expected Behaviour

I'd expect a single entry for every action.

Steps to Reproduce

I'm using the default configuration for the most part except the table name. That I changed to 'auto_audits'.
Also since its Laravel 8 I did not explicitly register the OwenIt\Auditing\AuditingServiceProvider::class.

use OwenIt\Auditing\Auditable as AuditingAuditable;
use OwenIt\Auditing\Contracts\Auditable;

class MyModel extends Model implements Auditable
{
    use CamelCaseAttributes;
    use AuditingAuditable;
...

I checked the query output using DB::enableQueryLog and DB::getQueryLog which shows 1 Update query and 2 inserts into the audits table with the same values.

Possible Solutions

Does the autoload somehow cause the event to be fired twice? Thats my only idea based on my findings.

Seems like you are registering OwenIt\Auditing\AuditingServiceProvider::class twice

But how?
Im not registering it manually at all.
I tried explicitly listing it in the app/config.php providers but the result is the same.

Im not registering it manually at all.

It is automatic

I tried explicitly listing it in the app/config.php providers

Worst

Thats why I didnt register it manually at first just to check if it would resolve the issue which it didnt.

So the automatic discovery somehow registers it twice if thats the issue.

It doesn't seem like a problem with this package, it seems like a problem on your implementation,
try a clean Laravel installation
try uninstalling and reinstalling the package,
upload an example repository with the bug/failing test so someone can review it
I have three applications with this package without problems

Will do so, thanks for the input

So, I tried some stuff,

  • Updated to Laravel 9 -> no change
  • Reinstalled the lib -> no change
  • setup test project -> works fine

Even after I removed the registration of the AuditingEventServiceProvider in the package, I still got 2 entries. Still puzzles me a bit since I expected none after that.

But yeah, as mentioned, some issue with the project so this can be closed.
thanks again for the input though!

Alright after debugging the issue for quite some time I noticed that the Auditing event was fired twice for every send.

The issue was the booted method in the model:

class MyModel extends Model implements Auditable
{
    use CamelCaseAttributes;
    use AuditingAuditable;

    protected static function booted()
    {
        static::addGlobalScope(new CustomScope());

        parent::booted(); // this line is the issue
    }
...

The Auditable trait was initialized twice since the laravel Model which is my base class recursivly initializes the traits on the class in its booted() method. This causes 2 auditing events and therefor 2 db entries.

Fix was to remove the parent::booted(); line completly since the constructor of the laravel Model::class is enough to set everything up.