0

Is there a way to include customization of ActivityLog

I'm looking for a way to included the events and properties affected in the description such that it shows specifics. For example User A updated their email address to abc@xyz.com. Considering that properties keeps track of the changes.

Also, is there a way to isolate the events fired? Like say, if a user creates and account.

I currently have the below code in my user model but i want for more customization

<?php

namespace App\Models;

use App\Notifications\MyWelcomeNotification;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Laravel\Jetstream\HasProfilePhoto;
use Laravel\Sanctum\HasApiTokens;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\CausesActivity;
use Spatie\Permission\Traits\HasRoles;
use Spatie\WelcomeNotification\ReceivesWelcomeNotification;

class User extends Authenticatable
{
    use HasApiTokens;
    use HasFactory;
    use HasProfilePhoto;
    use Notifiable;
    use TwoFactorAuthenticatable;
    use HasRoles;
    use ReceivesWelcomeNotification;
    use LogsActivity;
    use CausesActivity;
    

    /**
     * The attributes that are mass assignable.
     *
     * @var string[]
     */
    protected $fillable = [
        'name',
        'age',
        'gender',
        'fitness_level',
        'fitness_location',
        'fitness_goal',
        'height',
        'weight',
        'fitness_frequency',
        'email',
        'email_verified_at',
        'phone',
        'phone_verified_at',
        'password',
        'firebase_uid',
        'api_key',
        'device_token',
        'status',
        'subscription',
        'profile_photo_path',
        'referal_code',
    ];


    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
        'two_factor_recovery_codes',
        'two_factor_secret',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    /**
     * The accessors to append to the model's array form.
     *
     * @var array
     */
    protected $appends = [
        'profile_photo_url',
    ];

    public function sendWelcomeNotification(Carbon $validUntil)
    {
        $this->notify(new MyWelcomeNotification($validUntil));
    }

    public function getActivitylogOptions(): LogOptions { 
        return LogOptions::defaults()
                        ->logOnly([ 'name', 'age', 'fitness_level', 'fitness_location', 'fitness_goal', 'height', 'weight', 'fitness_frequency', 'email', 'phone', 'status', 'subscription'])
                        ->logOnlyDirty()
                        ->dontSubmitEmptyLogs()
                        ->setDescriptionForEvent(fn(string $eventName) => auth()->user()->name." {$eventName} ".$this->getDirty()); 
    }


    public function enrolInWorkout() {
        return $this->belongsToMany(Workout::class, 'user_workout', 'user_id', 'workout_id')->withTimestamps();
    }
}

I also know to use the below but it requires me to programmatically write the logs. I was looking for something more dynamic.

activity()
   ->performedOn($anEloquentModel)
   ->causedBy($user)
   ->withProperties(['customProperty' => 'customValue'])
   ->log('Look, I logged something');
Sharada Sharma
  • 129
  • 1
  • 11
  • 1
    https://spatie.be/docs/laravel-activitylog/v4/introduction has plenty of documentation to help you. If you want to get answers on SO you need to show us what you've tried already and we can help you get that to work. This is not a tutorial website it's a place to ask questions about issues with your code – apokryfos Dec 01 '22 at 08:56
  • @apokryfos please find my updated question with the implementation so far. – Sharada Sharma Dec 01 '22 at 09:28
  • In `setDescriptionForEvent` you should be able to get all model changes in `$this->getDirty()` – apokryfos Dec 01 '22 at 09:50
  • @apokryfos i no longer get any logs added in the activity_log table after implementing this. – Sharada Sharma Dec 03 '22 at 07:56
  • Not sure what you implemented. I only suggested something you might be able to call to find out what changed – apokryfos Dec 03 '22 at 08:53
  • @apokryfos I have updated the code to show the entire user model class. All I want to is to be able to log whatever operations the user performs on the user model and their the parameters these operations was performed on i.e if a user updates email, it should be logged. The current system logs the action without a causer so i cannot isolate operations based on individual user. – Sharada Sharma Dec 04 '22 at 05:17
  • `getDirty` returns an associative array with keys the fields that changed and values their new values so you'd need to convert that to string somehow (like e.g. `"User changed their ".collect($this->getDirty())->keys()->implode(',')." to ".collect($this->getDirty()->implode(','))` or something like that – apokryfos Dec 04 '22 at 07:34

0 Answers0