jeudi 10 mars 2022

Laravel Spatie - Custom Attribute value when audit log via modal

on my modal, I have two functions which I have used to log the data when it has been changed. those are below.

namespace App\Models;

use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Contracts\Activity;
use Illuminate\Support\Facades\Auth;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Receivinglogentry extends Model
{
    use HasFactory;
    use LogsActivity;

    protected $fillable = [
        'status',
        'amt_shipment',
        'container',
        'po',
        'etd_date',
        'eta_date',
    ];

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

    public function getActivitylogOptions(): LogOptions
    {
        return LogOptions::defaults()->logOnly(['*'])->logOnlyDirty();
    }

    public function tapActivity(Activity $activity,string $eventName)
    {
        $current_user = Auth::user()->name;
        $event        = $activity->attributes['event'];
        $data         = $activity->relations['subject']->attributes['container'];
        $masterID     = $activity->relations['subject']->attributes['id'];

        $activity->description   = "{$current_user} has {$event} Container : {$data}";
        $activity->causer_name   = $current_user;
        $activity->master_id     = $masterID ;
        $activity->log_name      = 'Receivinglogentry';
    }
}

fillable data status has been stored as an integer value. but I have to log it as a string value something like PENDING or ACTIVE. any recommendation to log attributes customizably is appricated.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire