Skip to content

Instantly share code, notes, and snippets.

@Yannx79
Last active July 26, 2024 22:22
Show Gist options
  • Save Yannx79/71484dd5dba80b4b6b08e4a0ed2be2e2 to your computer and use it in GitHub Desktop.
Save Yannx79/71484dd5dba80b4b6b08e4a0ed2be2e2 to your computer and use it in GitHub Desktop.
Method to obtain the changes of a model in Laravel
<?php
if (!function_exists('getChanges')) {
function getChanges(Model $model, $original = null)
{
$hiddenFields = ['FechaRegistro', 'FechaModificacion', 'rbupdate', 'updated_at', 'created_at', 'Id'];
$modifiedModel = $model->replicate();
$originalModel = $original ? $original : $model->refresh();
$changes = ' se ha cambiado ';
foreach ($originalModel->getAttributes() as $key => $originalValue) {
if (in_array($key, $hiddenFields)) continue;
$modifiedValue = $modifiedModel->getAttribute($key);
$changes .= $originalValue != $modifiedValue ? "[{$key}: de \"{$originalValue}\" a \"{$modifiedValue}\"], " : '';
}
return $changes;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment