I have a simple question, whenever I use Eloquent to edit a record in the DB, I'd like to be able to log the values of this row before and after the save.
My solution so far:
But by the time, the save method is called, the eloquent builder object has new values, the old ones have been overwritten.
Any ideas on how to do this ?
You can use the model events for this, there are many events like saving, updating, deleting ... for more details look here
class SomeModel extends Eloquent {
protected static function boot()
{
parent::boot(); // don't forget to call the parent boot method
//On saving
static::saving(
function($record)
{
$dirty = $record->getDirty();
foreach ($dirty as $field => $newdata)
{
$olddata = $record->getOriginal($field);
if ($olddata != $newdata)
{
// Do what it takes here :)
}
}
return true;
}
);
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With