Skip to content

Instantly share code, notes, and snippets.

@ctf0
Last active January 14, 2020 15:51
Show Gist options
  • Save ctf0/fa929221fb801e337ad0c03ac16b27c7 to your computer and use it in GitHub Desktop.
Save ctf0/fa929221fb801e337ad0c03ac16b27c7 to your computer and use it in GitHub Desktop.
  • we call the savePivotAudit directly to avoid errors of unknown model attributes

  • using get_class($model->$relationName()->getRelated()) to avoid error of null value

  • check if $pivotIds is avail b4 saving or we might get an error "edge case"

  • removed all the extra attributes like user, tags, url, ip, etc.. as they can be fetched from the main audit record

<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAuditsPivotTable extends Migration
{
/**
* Run the migrations.
*/
public function up()
{
Schema::create('audits_pivot', function (Blueprint $table) {
$table->increments('id');
$table->string('event');
$table->morphs('auditable');
$table->morphs('relation');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down()
{
Schema::dropIfExists('audits_pivot');
}
}
<?php
namespace App\Traits;
use Fico7489\Laravel\Pivot\Traits\PivotEventTrait;
use OwenIt\Auditing\Auditable;
/**
* https://gist.github.com/AbbyJanke/4d245b22dbcec277c207f033f37dae3b.
*/
trait MyAuditable
{
use Auditable, PivotEventTrait;
public static function bootMyAuditable()
{
static::pivotAttaching(function ($model, $relationName, $pivotIds, $pivotIdsAttributes) {});
static::pivotAttached(function ($model, $relationName, $pivotIds, $pivotIdsAttributes) {
if ($pivotIds) {
return $model->savePivotAudit(
'Attached',
get_class($model->$relationName()->getRelated()),
$pivotIds[0],
$model->getKey()
);
}
});
static::pivotDetaching(function ($model, $relationName, $pivotIds) {});
static::pivotDetached(function ($model, $relationName, $pivotIds) {
if ($pivotIds) {
return $model->savePivotAudit(
'Detached',
get_class($model->$relationName()->getRelated()),
$pivotIds[0],
$model->getKey()
);
}
});
static::pivotUpdating(function ($model, $relationName, $pivotIds, $pivotIdsAttributes) {});
static::pivotUpdated(function ($model, $relationName, $pivotIds, $pivotIdsAttributes) {});
}
private function savePivotAudit($eventName, $relationClass, $relationId, $modelId)
{
return app('db')->table('audits_pivot')->insert([
'event' => $eventName,
'auditable_id' => $modelId,
'auditable_type' => $this->getMorphClass(),
'relation_type' => $relationClass,
'relation_id' => $relationId,
'created_at' => now(),
'updated_at' => now(),
]);
}
/**
* normal : $model->audits
*/
private function getPivotAudits($type, $id)
{
return app('db')->table('audits_pivot')
->where('auditable_id', $id)
->where('auditable_type', $type)
->get()
->reverse();
}
/**
* with relation : $model->auditsWithRelation
*/
public function getAuditsWithRelationAttribute()
{
return $this->audits->map(function ($item) {
$item['relations'] = $this->getPivotAudits($item->auditable_type, $item->auditable_id);
return $item;
});
}
}
@tomscholz
Copy link

For anyone coming across this and wondering if it will work in combination with Nova. It doesn't.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment