Last active
May 28, 2020 07:46
-
-
Save rrpadilla/81f2f97da7dbc4fd8ce5c8fc67e936ab to your computer and use it in GitHub Desktop.
Fix Laravel Auditing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$smartAudit = new \App\SmartAudit($audit); | |
?> | |
@if (count($smartAudit->modifiedKeys)) | |
<!-- Card --> | |
<div class="card"> | |
<div class="card-header"> | |
<span> | |
{{ Str::ucfirst(__("{$audit->event} by")) }} | |
<b>{{ $audit->user ? $audit->user->name : __('Unknown') }}</b> | |
<small class="text-muted"> | |
- {{ $audit->created_at->format('F j, Y g:i A') }} | |
</small> | |
<small class="text-muted"> | |
({{ $audit->created_at->diffForHumans() }}) | |
</small> | |
</span> | |
</div> | |
<div class="card-body"> | |
<div class="table-responsive"> | |
<table class="table table-bordered"> | |
<thead> | |
<tr> | |
<th style="width: 150px;">{{ __('Attribute') }}</th> | |
<th> | |
<div class="row"> | |
<div class="col-6">{{ __('Old') }}</div> | |
<div class="col-6">{{ __('New') }}</div> | |
</div> | |
</th> | |
</tr> | |
</thead> | |
<tbody> | |
@foreach ($smartAudit->modifiedKeys as $attribute) | |
<tr> | |
<td> | |
<strong>@lang('auditable.field.'.$attribute)</strong> | |
</td> | |
<td> | |
<table class="w-100"> | |
<td style="width: 50%; background: #ffe9e9;"> | |
{{ optional($smartAudit->oldModel)->displayAuditField($attribute) }} | |
</td> | |
<td style="background: #e9ffe9;"> | |
{{ optional($smartAudit->newModel)->displayAuditField($attribute) }} | |
</td> | |
</table> | |
</td> | |
</tr> | |
@endforeach | |
</tbody> | |
</table> | |
</div> | |
</div> | |
</div> | |
<!-- Card --> | |
<div class="card"> | |
<div class="card-header">{{ __('Metadata') }}</div> | |
<div class="card-body"> | |
<table class="table table-bordered table-striped"> | |
<thead> | |
<th style="width: 150px;"> | |
{{ __('Meta') }} | |
</th> | |
<th> | |
{{ __('Info') }} | |
</th> | |
</thead> | |
<tbody> | |
<tr> | |
<td> | |
{{ __('IP address') }} | |
</td> | |
<td> | |
{{ $audit->ip_address }} | |
</td> | |
</tr> | |
<tr> | |
<td> | |
{{ __('User agent') }} | |
</td> | |
<td> | |
{{ $audit->user_agent }} | |
</td> | |
</tr> | |
<tr> | |
<td> | |
{{ __('URL') }} | |
</td> | |
<td> | |
{{ $audit->url }} | |
</td> | |
</tr> | |
</tbody> | |
</table> | |
</div> | |
</div> | |
@endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use OwenIt\Auditing\Contracts\Audit; | |
use Illuminate\Database\Eloquent\Model; | |
class SmartAudit | |
{ | |
/** | |
* @var array | |
*/ | |
public $modifiedKeys; | |
/** | |
* @var array | |
*/ | |
public $modified; | |
/** | |
* @var \Illuminate\Database\Eloquent\Model | |
*/ | |
public $oldModel; | |
/** | |
* @var \Illuminate\Database\Eloquent\Model | |
*/ | |
public $newModel; | |
/** | |
* @var \OwenIt\Auditing\Contracts\Audit | |
*/ | |
public $audit; | |
/** | |
* @param \OwenIt\Auditing\Contracts\Audit $audit | |
*/ | |
public function __construct(Audit $audit) | |
{ | |
$this->audit = $audit; | |
$auditableClassName = get_class($audit->auditable); | |
// Modified Auditable attributes | |
$this->modifiedKeys = []; | |
$this->modified = []; | |
if (count($audit->new_values)) { | |
$this->newModel = new $auditableClassName(); | |
$this->newModel->setRawAttributes($audit->new_values, true); | |
foreach ($audit->new_values as $attribute => $value) { | |
$this->modified[$attribute]['new'] = $value; | |
} | |
} | |
if (count($audit->old_values)) { | |
$this->oldModel = new $auditableClassName(); | |
$this->oldModel->setRawAttributes($audit->old_values, true); | |
foreach ($audit->old_values as $attribute => $value) { | |
$this->modified[$attribute]['old'] = $value; | |
} | |
} | |
$this->modifiedKeys = array_keys($this->modified); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment