owen-it / laravel-auditing

Record the change log from models in Laravel
https://laravel-auditing.com
MIT License
2.94k stars 382 forks source link

Column with type `point` throws error #908

Closed weogrim closed 3 months ago

weogrim commented 3 months ago
Q A
Bug? yes
New Feature? no
Framework Laravel
Framework version 10.10
Package version 13.6
PHP version 8.1

Actual Behaviour

I have field in DB storing spatial data, and more specifically point type in MySQL. When I create model or edit this field, it is not stored on audits table (when update only this field, new_values and old_values is empty array). When try to delete model, i get exception: Unable to encode attribute [old_values] for model [OwenIt\Auditing\Models\Audit] to JSON: Malformed UTF-8 characters, possibly incorrectly encoded. in ./vendor/laravel/framework/src/Illuminate/Database/Eloquent/JsonEncodingException.php

Expected Behaviour

Somehow, store spatial data change.

Steps to Reproduce

  1. Create table with column with type Point.
  2. Create model for this table
  3. Do basic setup of auditing for this model.
  4. Try to edit point field, you will get empty arrays.
  5. Try to delete row, you get exception

If this is expected behavior, can you point me to some idea how to fix this in my application? Unfortunately i cannot skip this field with $auditExclude, i need this data history. I can split the point field into two decimal fields lat and long, but if there is any solution, I would like to be able to use the point type

parallels999 commented 3 months ago

I haven't used that data type, did you try adding a custom cast to the model? https://laravel.com/docs/10.x/eloquent-mutators#custom-casts

protected $casts = [
    'your_point_field' => YourPointCustomCaster::class,
];