我不想将更新存储在名为audits_pivot
.
为此,我需要挂钩attached
模型(状态)上的事件,正如我发现的那样,它实际上并不 存在。我能做的是监听要调用的自定义数据透视类(LicenceState),因为这相当于“附加”。不幸的是,回调不包含有关枢轴附加到什么的任何信息。static::saving
static::saving
有来自 fico7489 的类似的库,但它不能与我正在使用的Laravel Nova一起使用。
如何访问数据透视行附加到的模型的名称和 ID 等内容?
<?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 {}