Laravel 活动日志在更新时不起作用

bha*_*han 2 php logging laravel activitylog laravel-8

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

我的模态

<?php

namespace App\Models;

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

class Contact extends Model
{
    use HasFactory, LogsActivity;
    protected $fillable = [
        'comp_name',
        'cont_name',
        'cont_email',
        'cont_number',
        'created_by',
        'updated_by',
    ];
        // spatie activitylog
    protected static $logFillable = true;
    protected static $logOnlyDirty = true;
    protected static $logName='users'; // default
}
Run Code Online (Sandbox Code Playgroud)

我的控制器

 Contact::where('id',$input['id'])->update($data);
 $retrnArray = array('status'=>1,'msg'=>'Updated Successfully');
Run Code Online (Sandbox Code Playgroud)

JEX*_*JEX 5

写入数据库但不写入 ActivityLogs的更新将如下所示:

User::where("id", 1)->update($data);
Run Code Online (Sandbox Code Playgroud)

在 DB 中写入并在 ActivityLogs 中写入的更新如下所示:

User::where("id", 1)->first()?->update($data); //if you are using php >= 8
User::where("id", 1)->firstOrFail()->update($data); // Good using php >= 5.6
User::find(1)?->update($data); //This also works as find("your id") also returns the model that it was able to found, it also returns null so check it on null or use nullsafe operator.
Run Code Online (Sandbox Code Playgroud)

加载模型以正确生成 ActivityLog 非常重要。