我不会将更新存储在名为audits_pivot
的单独表中的数据透视表中。
要做到这一点,我需要对模型(状态)上的attached
事件进行某种程度的了解,正如我发现的doesn't really存在一样。我可以做的是在自定义枢纽类(LicenceState)上侦听要调用的static::saving
,因为这等效于“ attached”。不幸的是,static::saving
的回调没有包含有关数据透视表所附加内容的任何信息。
有些像来自fico7489的this one库,但是不能与我正在使用的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 {}
答案 0 :(得分:1)
Accountant软件包可以满足您的需求。
通过使用Eventually,它支持attach()
,detach()
,updateExistingPivot()
,sync()
的事件,支持多对多关系(即数据透视表)和toggle()
。
甚至不需要使用custom intermediate models。
文档涵盖了安装,配置和使用的所有方面。