Search code examples
eloquentspatie-activitylog

How to Implement Soft Deletes with Laravel Activity Log?


I'm using the Spatie Laravel Activitylog package, and I want to log soft delete actions performed on my models. However, I'm having trouble getting the logs to correctly capture the deletion events. What is the best way to set this up in Laravel?

I tried configuring the Spatie Laravel Activitylog to log soft delete events by using model observers and adding the Loggable trait to my models. I expected the soft delete actions to be captured in the activity log. However, the log entries are not recording the delete actions as expected.


Solution

  •  $designationType = Designation::find($designationId);
                if ($designationType) {
                    $designationType->delete();
    

    Here, soft delete will get logged perfectly as the find is directly calling the eloquent orm.

    Observation::where('id', $id)->firstOrFail()->delete();
    

    Here I have to use firstOrFail if there is where clause otherwise the deletion isn't logged.