Skip to content

Instantly share code, notes, and snippets.

@joetannenbaum
Created January 30, 2017 16:39
Show Gist options
  • Save joetannenbaum/b2654ed64d1f5102bd0cc32109d8bfed to your computer and use it in GitHub Desktop.
Save joetannenbaum/b2654ed64d1f5102bd0cc32109d8bfed to your computer and use it in GitHub Desktop.
<?php
trait TracksHistoryTrait
{
protected function track(Model $model, callable $func = null, $table = null, $id = null)
{
// Allow for overriding of table if it's not the model table
$table = $table ?: $model->getTable();
// Allow for overriding of id if it's not the model id
$id = $id ?: $model->id;
// Allow for customization of the history record if needed
$func = $func ?: [$this, 'getHistoryBody'];
// Get the dirty fields and run them through the custom function, then insert them into the history table
$this->getUpdated($model)
->map(function ($value, $field) use ($func) {
return call_user_func_array($func, [$value, $field]);
})
->each(function ($fields) use ($table, $id) {
History::create([
'reference_table' => $table,
'reference_id' => $id,
'actor_id' => Auth::user()->id,
] + $fields);
});
}
protected function getHistoryBody($value, $field)
{
return [
'body' => "Updated {$field} to ${value}",
];
}
protected function getUpdated($model)
{
return collect($model->getDirty())->filter(function ($value, $key) {
// We don't care if timestamps are dirty, we're not tracking those
return !in_array($key, ['created_at', 'updated_at']);
})->mapWithKeys(function ($value, $key) {
// Take the field names and convert them into human readable strings for the description of the action
// e.g. first_name -> first name
return [str_replace('_', ' ', $key) => $value];
});
}
}
@xaviqv
Copy link

xaviqv commented Feb 8, 2019

Thanks for sharing this script! It works great! But it requires some patches in the way to have it running:

  1. Add usage of History model
  2. Add Auth model or replace "Auth::" in line 23 with "auth()"
  3. Line 5: "track(Model $model," replaced with "track($model,"

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