laravelactivitylog

Laravel Activity Log won't work on Update and Delete


I'm using SPATIE laravel-activitylog I followed all the instructions but still, it only logs the Create function not update and delete while using it on a Modal

My Modal

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Spatie\Activitylog\Traits\LogsActivity;

class z_education extends Model
{
    //
    use LogsActivity;

    protected $fillable = [
        'user_id',
        'type',
        'school_name',
        'degree',
        'isremoved',
    ];
    protected static $logFillable = true;

}

My Controller

    public function delete_user_education($id)
    {
        z_education::where('id', $id)->delete();
        return back();
    }

Solution

  • Your controller query is executed via the Query Builder, instead of an Eloquent Model. So there will be no model events to listen to.

    Retrieve and delete the model itself to fire and log the events:

    $model = z_education::findOrFail($id);
    $model->delete();
    
    return back();