Skip to content

Instantly share code, notes, and snippets.

@ctf0
Last active January 14, 2020 15:51
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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;
});
}
}
@PrafullaKumarSahu
Copy link

I need to implement something in Laravel 5.6 with Laravel auditing 8.0 and current laravel-pivot, I have configured my application as you suggested, but not using the trait in base Model, I have video_status pivot where I want to implement this, so should I use it in Video and Status model or VideoStatus pivot? also, I would like to know, if I need to save the data manually to the audit_pivots table or it will save directly?

@ctf0
Copy link
Author

ctf0 commented Apr 26, 2019

sorry for the late reply, github still doesnt notify users of gist activities :(

anyway am using the same solution in a package i made call odin and am simply including that trait in every model i want to have revisions of

so an answer to ur question i would probably suggest adding the trait to both ur Video and Status models
or if u have the laravel v5.8 then modify the above logic to work with the pivot events https://laravel.com/docs/5.8/releases

@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