Php Larvel如何访问自定义pivot类上的关系数据
我不想将更新存储在名为Php Larvel如何访问自定义pivot类上的关系数据,php,laravel,eloquent,laravel-nova,laravel-auditing,Php,Laravel,Eloquent,Laravel Nova,Laravel Auditing,我不想将更新存储在名为audits\u pivot的单独表中的pivot表中 要做到这一点,我需要对模型(状态)上的附加的事件进行挂钩排序,我发现它是存在的。我所能做的是监听要调用的自定义pivot类(LicenceState)的static::saving,因为这相当于“attached”。不幸的是,static::saving的回调没有包含任何有关pivot连接到的内容的信息 有一些类似于fico7489的库,但我正在使用它们,它们不能一起工作 如何访问数据透视行附加到的模型的名称和Id &
audits\u pivot
的单独表中的pivot表中
要做到这一点,我需要对模型(状态)上的附加的事件进行挂钩排序,我发现它是存在的。我所能做的是监听要调用的自定义pivot类(LicenceState)的static::saving
,因为这相当于“attached”。不幸的是,static::saving
的回调没有包含任何有关pivot连接到的内容的信息
有一些类似于fico7489的库,但我正在使用它们,它们不能一起工作
如何访问数据透视行附加到的模型的名称和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 {}
软件包可以满足您的需要
它通过使用支持多对多关系(即透视表),从而为attach()
、detach()
、updateExistingPivot()
、sync()
和toggle()
添加事件
甚至不需要使用
文档涵盖了安装、配置和使用的所有方面。我已经尝试过使用它,但无法使其正常工作。当我在状态
模型上使用最终
和可记录
特征时,不会记录透视事件,而是将多行添加到分类账表中,其中事件被“检索”,且“修改”列为空。除此之外,当许可证
模型还具有可记录
特征时,一个附件一次最多创建40个分类账分录。我应该在GitLab上打开票据吗?配置时您可能遗漏了一些内容。当然,如果你认为这是一个错误,就打开一张罚单。