Я не хочу хранить обновления в сводных таблицах внутри отдельной таблицы с именем audits_pivot
.
. Для этого мне нужно разобраться в событии attached
в модели (State), который, как я выяснил, не на самом деле не существует. Что я могу сделать, так это прослушивать пользовательский сводный класс (LicenceState) для вызова static::saving
, поскольку это эквивалентно «attach». К сожалению, обратный вызов static::saving
не содержит никакой информации о том, к чему был присоединен сводный элемент.
Есть библиотеки вроде этой от fico7489, но они не работают вместе с Laravel Nova , который я использую.
Как получить доступ к таким вещам, как имя и идентификатор модели, к которой была прикреплена сводная строка?
<?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 {}