Laravel 活动日志不适用于更新和删除

问题描述 投票:0回答:2

我正在使用 SPATIE laravel-activitylog 我遵循了所有说明,但它仍然只记录创建功能,而不是在模态上使用它时更新和删除

我的模态

<?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;

}

我的控制器

    public function delete_user_education($id)
    {
        z_education::where('id', $id)->delete();
        return back();
    }
laravel activitylog
2个回答
3
投票

您的控制器查询是通过查询生成器执行的,而不是 Eloquent 模型。所以不会有模型事件可供监听。

检索并删除模型本身以触发并记录事件:

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

return back();

0
投票

要在 Laravel 中使用 Eloquent 执行更新或删除操作时保存日志活动,您可以利用 Eloquent 事件,例如更新和删除。这些事件允许您拦截模型的生命周期并在执行操作之前记录活动。

实施示例 创建日志模型(如果尚未创建):为日志记录活动创建模型和迁移。

php artisan make:model ActivityLog -m

更新迁移文件以包含必要的字段,例如

action
model_type
model_id
changes
performed_by

public function up() {
    Schema::create('activity_logs', function (Blueprint $table) {
        $table->id();
        $table->string('action');
        $table->string('model_type');
        $table->unsignedBigInteger('model_id');
        $table->json('changes')->nullable();
        $table->unsignedBigInteger('performed_by')->nullable(); // Nullable for system actions
        $table->timestamps();
    });
}

运行迁移:

php artisan migrate

设置 Eloquent 事件:在要跟踪更新和删除的 Eloquent 模型中,使用 boot 方法定义更新和删除事件处理程序。

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Models\ActivityLog;
use Illuminate\Support\Facades\Auth;

class YourModel extends Model {
    protected static function boot() {
        parent::boot();

        static::updating(function ($model) {
            ActivityLog::create([
                'action' => 'updated',
                'model_type' => get_class($model),
                'model_id' => $model->id,
                'changes' => json_encode([
                    'old' => $model->getOriginal(),
                    'new' => $model->getDirty(),
                ]),
                'performed_by' => Auth::id(),
            ]);
        });

        static::deleting(function ($model) {
            ActivityLog::create([
                'action' => 'deleted',
                'model_type' => get_class($model),
                'model_id' => $model->id,
                'changes' => json_encode([
                    'deleted' => $model->toArray(),
                ]),
                'performed_by' => Auth::id(),
            ]);
        });
    }
}

可选:创建辅助特征:如果您需要跨多个模型使用此功能,请创建可重用特征。

namespace App\Traits;

use App\Models\ActivityLog;
use Illuminate\Support\Facades\Auth;

trait LogsActivity {
    protected static function bootLogsActivity() {
        static::updating(function ($model) {
            ActivityLog::create([
                'action' => 'updated',
                'model_type' => get_class($model),
                'model_id' => $model->id,
                'changes' => json_encode([
                    'old' => $model->getOriginal(),
                    'new' => $model->getDirty(),
                ]),
                'performed_by' => Auth::id(),
            ]);
        });

        static::deleting(function ($model) {
            ActivityLog::create([
                'action' => 'deleted',
                'model_type' => get_class($model),
                'model_id' => $model->id,
                'changes' => json_encode([
                    'deleted' => $model->toArray(),
                ]),
                'performed_by' => Auth::id(),
            ]);
        });
    }
}

在模型中使用该特征:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Traits\LogsActivity;

class YourModel extends Model {
    use LogsActivity;
}

显示日志(可选):您现在可以查询 Activity_logs 表以检索特定模型的日志。

$logs = ActivityLog::where('model_type', YourModel::class)->get();
© www.soinside.com 2019 - 2024. All rights reserved.