Open rrpadilla opened 4 years ago
Hi, in case you want to use a lighter version of this package. Laravel Smart Audit
Thanks.
Is there a fix for this?
I hit a very similar problem.
I opened a bug report with laravel because IMHO the it's sub-optimal that there method returns different results based on a (IMHO) wrong cache-hit.
IMO its not ideal to store casted values in audits at all. We are working on a solution, but have some backwards compatibility to consider as well.
Note, related to this: https://github.com/owen-it/laravel-auditing/discussions/799
Actual Behaviour
Following your documentation: "The getModified() method honors all attribute mutators and/or casts defined in the Auditable model, transforming the data accordingly." The issue is when casting to value objects. See laravel documentation. When this method is calling "getFormattedValue" laravel will get the value from the classCastCache property. See: https://github.com/laravel/framework/blob/master/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php#L562
$modified['old'] will be equal to $modified['new'] if your attribute is using a custom cast.
Expected Behaviour
$modified['old'] should NOT be equal to $modified['new'] if the attribute is using a custom cast.
Steps to Reproduce
1- First create a cast class
2- Add the cast to the model
3- Update the model
4- In your view call this.
and you will see: 'My Address Value' instead of 'Updated Address Value'.
Possible Solutions
This package should not call mutators and casters to alter the values. Every Auditable model should be able to transform itself when you create a new instance of the model passing the new attributes.
This is what I'm doing. See gist. Basically creating a class that will create the oldAuditableModel and newAuditableModel. And laravel will take care of mutators and casting. The setRawAttributes function does the trick here.
2- Add a method displayAuditField to the Auditable model to return the proper value. This method is way to sanitize the input and avoid errors.
3- Then in your view you can do this
Image: