I'm using laravel-activitylog.
When a record is deleted the $activity->subject
is null
.
On updated
and created
the $activity->subject
returns the model object.
Is there any way I can return the subject on deleted
too?
My Answer:
Unfortunately, the subject is set to null when a record is deleted in the laravel-activitylog package. This is because the record no longer exists in the database, so there is no way to retrieve the model object.
One workaround for this issue is to store the model data in the activity log before deleting the record. You can create a custom method in your model to store the data before deletion, and then retrieve it when needed.
Here is an example of how you can achieve this:
1. Create a custom method in your model to store the data before deletion:
php
public function storeActivityLogBeforeDelete()
{
$this->activityLogData = $this->toArray();
$this->save();
}
2. Override the delete method in your model to call the custom method before deleting the record:
php
public function delete()
{
$this->storeActivityLogBeforeDelete();
parent::delete();
}
3. Retrieve the stored data in your activity log:
php
$activity = Activity::where('subject_id', $deletedModelId)
->where('subject_type', get_class($deletedModel))
->where('description', 'deleted')
->latest()
->first();
$deletedModelData = $activity->subject->activityLogData;
By following these steps, you can store the model data before deletion and retrieve it when needed in the activity log. This way, you can still access the subject data even after it has been deleted.
Rate this post
3 of 5 based on 4459 votesComments