1

I wan't to store updates in pivot tables inside a separate table called audits_pivot.

To do that I need to sort of hook into the attached event on the model (State), which as I found out doesn't really exist. What I can do is to listen on the custom pivot class (LicenceState) for static::saving to be called, since that is the equivalent to 'attached'. Unfortunately does the callback of static::saving not contain any information about what the pivot was attached to.

There are libraries like this one from fico7489, but that doesn't work together with Laravel Nova, which I'm using.

How can I access things like the name and Id of the Model that the pivot row was attached to?

<?php

namespace App;

use Illuminate\Database\Eloquent\Model as EloquentModel;
use Illuminate\Database\Eloquent\Relations\Pivot as EloquentPivot;
use OwenIt\Auditing\Auditable as AuditableTrait;
use OwenIt\Auditing\Contracts\Auditable;

abstract class Pivot extends EloquentPivot implements Auditable
{
    use AuditableTrait;

    public static function boot()
    {
        parent::boot();

        static::saving(function ($model)  {
            // How can I access here things like the name and Id of the Model that the pivot row was attached to?
            // What I'm looking for is basically this: 
            // savePivotAudit('attached', 12, 'App\Licence', 'App\State', 51, '2020-01-14 13:55:58');
        });
    }

    private function savePivotAudit($eventName, $id, $relation, $pivotId, $date)
    {
        return app('db')->table('audits_pivot')->insert([
            'event' => $eventName,
            'auditable_id' => $id,
            'auditable_type' => $this->getMorphClass(),
            'relation_id' => $pivotId,
            'relation_type' => $relation,
            'parent_updated_at' => $date,
        ]);
    }
}

class License extends EloquentModel {}

class State extends EloquentModel
{
    use AuditableTrait;


    public function licenses()
    {
        return $this->belongsToMany(License::class)
            ->using(LicenseState::class);
    }
}

class LicenseState extends Pivot {}
Tom
  • 4,070
  • 4
  • 22
  • 50

1 Answers1

1

The Accountant package does what you want.

It supports many to many relations (i.e. pivot tables), by using Eventually, which adds events for attach(), detach(), updateExistingPivot(), sync() and toggle().

There's not even a need for using custom intermediate models.

The documentation covers all aspects of installation, configuration and usage.

Quetzy Garcia
  • 1,820
  • 1
  • 21
  • 23
  • I tried using it already, but I couldn't get it to work. Pivot events aren't recorded when I use the `Eventually` and `Recordable` traits on the `State` model, but instead it adds multiple rows to the ledgers table where the event is 'retrieved' and the 'modified' column is empty. In addition to that, when the `Licence` model also has the `Recordable` trait, one attach creates up to 40 ledger entries at once. Should I open a ticket on GitLab? – Tom Jan 15 '20 at 11:45
  • You probably missed something when configuring. Sure, open a ticket if you think it's a bug. – Quetzy Garcia Jan 15 '20 at 12:06