(Update) Audits Trait 🚀

- stop logging system actions
This commit is contained in:
HDVinnie
2019-11-14 10:12:24 -05:00
parent 16847d4474
commit 3bd982e660

View File

@@ -145,21 +145,25 @@ trait Auditable
*/
protected static function registerCreate($model)
{
// Generate the JSON to store
$data = self::generate('create', [], self::strip($model, $model->getAttributes()));
// Get auth (if any)
$userId = self::getUserId();
// Store record
$now = Carbon::now()->format('Y-m-d H:i:s');
DB::table('audits')->insert([
'user_id' => $userId,
'model_name' => class_basename($model),
'model_entry_id' => $model->{$model->getKeyName()},
'action' => 'create',
'record' => $data,
'created_at' => $now,
'updated_at' => $now,
]);
if ($userId !== 1) {
// Generate the JSON to store
$data = self::generate('create', [], self::strip($model, $model->getAttributes()));
// Store record
$now = Carbon::now()->format('Y-m-d H:i:s');
DB::table('audits')->insert([
'user_id' => $userId,
'model_name' => class_basename($model),
'model_entry_id' => $model->{$model->getKeyName()},
'action' => 'create',
'record' => $data,
'created_at' => $now,
'updated_at' => $now,
]);
}
}
/**
@@ -169,22 +173,26 @@ trait Auditable
*/
protected static function registerUpdate($model)
{
// Strip data
// Generate the JSON to store
$data = self::generate('update', self::strip($model, $model->getOriginal()), self::strip($model, $model->getChanges()));
// Get auth (if any)
$userId = self::getUserId();
// Store record
$now = Carbon::now()->format('Y-m-d H:i:s');
DB::table('audits')->insert([
'user_id' => $userId,
'model_name' => class_basename($model),
'model_entry_id' => $model->{$model->getKeyName()},
'action' => 'update',
'record' => $data,
'created_at' => $now,
'updated_at' => $now,
]);
if ($userId !== 1) {
// Strip data
// Generate the JSON to store
$data = self::generate('update', self::strip($model, $model->getOriginal()), self::strip($model, $model->getChanges()));
// Store record
$now = Carbon::now()->format('Y-m-d H:i:s');
DB::table('audits')->insert([
'user_id' => $userId,
'model_name' => class_basename($model),
'model_entry_id' => $model->{$model->getKeyName()},
'action' => 'update',
'record' => $data,
'created_at' => $now,
'updated_at' => $now,
]);
}
}
/**
@@ -194,20 +202,24 @@ trait Auditable
*/
protected static function registerDelete($model)
{
// Generate the JSON to store
$data = self::generate('delete', self::strip($model, $model->getAttributes()));
// Get auth (if any)
$userId = self::getUserId();
// Store record
$now = Carbon::now()->format('Y-m-d H:i:s');
DB::table('audits')->insert([
'user_id' => $userId,
'model_name' => class_basename($model),
'model_entry_id' => $model->{$model->getKeyName()},
'action' => 'delete',
'record' => $data,
'created_at' => $now,
'updated_at' => $now,
]);
if ($userId !== 1) {
// Generate the JSON to store
$data = self::generate('delete', self::strip($model, $model->getAttributes()));
// Store record
$now = Carbon::now()->format('Y-m-d H:i:s');
DB::table('audits')->insert([
'user_id' => $userId,
'model_name' => class_basename($model),
'model_entry_id' => $model->{$model->getKeyName()},
'action' => 'delete',
'record' => $data,
'created_at' => $now,
'updated_at' => $now,
]);
}
}
}