Last active
July 26, 2024 22:22
-
-
Save Yannx79/71484dd5dba80b4b6b08e4a0ed2be2e2 to your computer and use it in GitHub Desktop.
Method to obtain the changes of a model in Laravel
This file contains hidden or 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 | |
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