Ad

Why Laravel Auditing Not Functioning?

I have done my coding but the laravel audit seems not catch the data for update. The data i try to update is on the column of value, but on the database the value are not be catch on audit table for auditing the update changes . below is the coding for update the data.

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);
        }
    }
}
Ad

Answer

Not sure if this will help but,

i see you have the following in the transform method:

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

in the documentation i see the following:

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

the above is to record the audit by transforming the role_id to the role_name, enabling you to capture the role_name as well.

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

Ad
source: stackoverflow.com
Ad