Hi, Is there a way that I can extend the Audit Model ? I want to add custom fields to the table audits.
For example version_number, table_name, etc.
I just have to change the migration?
Thanks
Yeah, that should be fairly straightforward.
Just update the migration by adding the new columns and extend the Audit class (MyAudit or something) and add the logic to store the table name and keep track of the version attribute.
That should be about it.
Thanks man!!!
Marking as closed.
I did what you told me too, but I found an problem, because the fields of the audit table are hardcode in the trait in the toAudit method:
public function toAudit()
{
if (!$this->isEventAuditable($this->auditEvent)) {
throw new RuntimeException('A valid audit event must be set');
}
$method = 'audit'.Str::studly($this->auditEvent).'Attributes';
if (!method_exists($this, $method)) {
throw new RuntimeException(sprintf(
'Unable to handle "%s" event, %s() method missing',
$this->auditEvent,
$method
));
}
$this->updateAuditExclusions();
$old = [];
$new = [];
$this->{$method}($old, $new);
return $this->transformAudit([
'old_values' => $old,
'new_values' => $new,
'event' => $this->auditEvent,
'auditable_id' => $this->getKey(),
'auditable_type' => $this->getMorphClass(),
'user_id' => $this->resolveUserId(),
'url' => $this->resolveUrl(),
'ip_address' => $this->resolveIpAddress(),
'created_at' => $this->freshTimestamp()
]);
}
So I had to create other trait adding the extra fields:
public function toAudit()
{
if (!$this->isEventAuditable($this->auditEvent)) {
throw new RuntimeException('A valid audit event must be set');
}
$method = 'audit'.Str::studly($this->auditEvent).'Attributes';
if (!method_exists($this, $method)) {
throw new RuntimeException(sprintf(
'Unable to handle "%s" event, %s() method missing',
$this->auditEvent,
$method
));
}
$this->updateAuditExclusions();
$old = [];
$new = [];
$this->{$method}($old, $new);
return $this->transformAudit([
'old_values' => $old,
'new_values' => $new,
'event' => $this->auditEvent,
'auditable_id' => $this->getKey(),
'auditable_type' => $this->getMorphClass(),
'user_id' => $this->resolveUserId(),
'url' => $this->resolveUrl(),
'ip_address' => $this->resolveIpAddress(),
'created_at' => $this->freshTimestamp(),
'auditable_table' => $this->getTable(),
'version' => $this->getVersion()
]);
}
And in the model I hda to do this (it's not very clean):
use OwenIt\Auditing\Auditable;
use App\Audits\CustomAuditable;
class Country extends Model implements AuditableContract
{
use Auditable, CustomAuditable {
CustomAuditable::toAudit insteadof Auditable;
}
. . .
}
Is there a better way to do this? Thanks
Hi,
You don't have to create a new trait and override the toAudit() method just to add more attributes. If you override the transformAudit() method instead, you can then add whatever you need to the audit array.
See this.
Thanks for the reply!. I did that just with this code in the model Country:
public function transformAudit(array $data)
{
Arr::set($data, 'auditable_table', $this->getTable());
Arr::set($data, 'version', $this->version);
return $data;
}
Can you be more explicit ! i have the same problem
More explicit about what?
This can be done in AppServiceProvider@boot;
Audit::creating(function (Audit $model) { $model->custom_field = 'test' });
I have done it in a middleware as the AppServiceProvider cannot access session variables. Thanks!
Most helpful comment
This can be done in
AppServiceProvider@boot;Audit::creating(function (Audit $model) { $model->custom_field = 'test' });