owen-it / laravel-auditing

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

SoftDelete behaviour config option true/false #909

Closed dhcmega closed 6 months ago

dhcmega commented 6 months ago

Hi I know that this has been discussed before: https://github.com/owen-it/laravel-auditing/issues/256

But, do you think it might be possible to have a "config" option for setting the behaviour of the delete? if audits.softdelete is true, then only update de deleted_at value, if false, then keep the current behaviour.

Thanks!

parallels999 commented 6 months ago

Did you try updating the settings globally at the config/audit.php file and set the 'timestamps' => false to true?

if audits.softdelete is true, then only update de deleted_at value, if false, then keep the current behaviour.

do you want the fields values($old) not to be audited in SoftDelete?

dhcmega commented 6 months ago

@parallels999 thanks for getting back to me. I tried timestamp flag and it adds the timestamp to the audit.

Yes, if a model is softDeleted I rather only update the deleted_at field. Does it makes sense? Thanks!

parallels999 commented 6 months ago

Does it makes sense?

Not much

Did you try transformAudit?

use Illuminate\Database\Eloquent\SoftDeletes;

///
public function transformAudit(array $data): array
{
    if ($this->auditEvent === 'deleted' && in_array(SoftDeletes::class, class_uses(get_class($this)))) {
        $data['old_values'] = [];
        $data['new_values'] = [];
    }

    return $data;
}
dhcmega commented 6 months ago

hi @parallels999 would I need to add that method to all models that use audit? Can I modify it for all models in one place? thank you

parallels999 commented 6 months ago

I use an abstract model, and all my models inherit from that