为什么 laravel 审计不工作?

Why laravel auditing not functioning?

我已经完成了编码,但 laravel 审计似乎没有捕获要更新的数据。 我尝试更新的数据位于值列中,但在数据库中,该值未在审计 table 中捕获以用于审计更新更改。 下面是更新数据的代码。

academicsetting.php:

class AcademicSetting extends Model implements Auditable
{
    use SoftDeletes, ActiveScope;
    use \OwenIt\Auditing\Auditable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $dates    = ['deleted_at'];
    protected $fillable = [
        'id',
        'type',
        'field',
        'name',
        'value',
        'description',
        'is_active',
        'created_by',
        'created_at',
        'updated_by',
        'updated_at',
        'deleted_by',
        'deleted_at',
    ];

    protected $casts = [
        'value' => 'json',
    ];

    public function transformAudit(array $data): array
    {
        dump($data);exit();
        if ($data['event'] == 'created') {

            $data['new_values']['created_by'] = Staff::where('id', $this->getAttribute('created_by'))->value('name');
            $data['new_values']['updated_by'] = Staff::where('id', $this->getAttribute('updated_by'))->value('name');

        }
        if ($data['event'] == 'deleted') {
            $data['old_values']['deleted_by'] = Staff::where('id', $this->getOriginal('deleted_by'))->value('name');
            $data['new_values']['deleted_by'] = Staff::where('id', $this->getAttribute('deleted_by'))->value('name');
        }
        if ($data['event'] == 'updated') {
            $data['old_values']['updated_by'] = Staff::where('id', $this->getOriginal('updated_by'))->value('name');
            $data['new_values']['updated_by'] = Staff::where('id', $this->getAttribute('updated_by'))->value('name');

        }

        return $data;
    }
}

academicgeneralcontroller.php:

 public function update(Request $request, $id)
    {
        /** implode multi dropdown value */
        $request['final_attendance_reset_mark'] = $request->input('final_attendance_reset_mark');
        /** end */
        $this->general->update($request->except('_token', 'academicsetting-table_length', '_method'), $id);
        Session::flash('alert-success', msg('msg_success_update'));
        return redirect()->route('academic_general.index');
    }

generalrepository.php:

class GeneralRepository extends Repository
{

    /**
     * Specify Model class name
     *
     * @return mixed
     */
    public function model()
    {
        return AcademicSetting::class;
    }

    public function update(array $data, $id, $attribute = 'id')
    {
        // dump($data);exit();
        foreach ($data as $key => $value) {
            if ($key == 'final_attendance_reset_mark') {

                $array = [];
                if (!empty($value)) {
                    foreach ($value as $key_value => $value) {
                        $array[(int) $key_value] = (int) $value;
                    }
                }

                $json = json_encode($array, JSON_FORCE_OBJECT);
                // dump($json);
                $update_value = [
                    'value' => $json,
                ];
            } else {
                $update_value = ['value' => $value];
            }
            dump($update_value);exit();
            $general = AcademicSetting::where('field', $key)->update($update_value);
        }
    }
}

不确定这是否有帮助,但是

我看到你在转换方法中有以下内容:

 $data['new_values']['created_by'] = Staff::where('id', $this->getAttribute('created_by'))->value('name');

在文档中我看到以下内容:

$data['old_values']['role_name'] = Role::find($this->getOriginal('role_id'))->name;

以上是通过将 role_id 转换为 role_name 来记录审核,使您也可以捕获 role_name。

https://laravel-auditing.herokuapp.com/docs/4.1/audit-transformation