Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

shariff's avatar
Level 50

Has anyone use this plugin noxoua/filament-activity-log?

Hello everyone

I'm using this package to log activities for few models. It is working fine for normal model. When I try to use this for relationship it is not working. In my case I have one User model and Contact model. This has one to many relationship.

I'm trying to achieve this https://activity-log.noxo.app/fields#relation but it is not working.

User model

use HasFactory, InteractsWithMedia, LogsActivity;

public function contacts()
    {
        return $this->hasMany(Contact::class);
    }

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

Contact model

public function user()
    {
        return $this->belongsTo(User::class);
    }

User Logger

public static function resource(ResourceLogger $logger): ResourceLogger
    {
        return $logger
            ->preloadRelations('contacts')
            ->fields([
                Field::make('name')
                    ->label(('Name')),
                Field::make('domain')
                    ->label(('Domain')),
                Field::make('email')
                    ->label(('Email')),
                Field::make('phone')
                    ->label(('Phone')),

                Field::make('contacts')
                    ->hasMany('contacts')
                    ->table([
                        Field::make('name'),
                        Field::make('email'),
                        Field::make('phone'),
                    ])
                    ->label('Contacts'),
            ])

but this is not working if I add any contact for any user. It is working only with user changes. Is anything I'm missing here?

Thanks

0 likes
1 reply
LaryAI's avatar
Level 58

To address the issue of logging activities for related models using the noxoua/filament-activity-log package, you need to ensure that the activity log is properly configured to capture changes in the related models. Here’s a step-by-step solution to achieve this:

  1. Ensure the LogsActivity trait is used in both models: Make sure both the User and Contact models use the LogsActivity trait.

  2. Configure the getActivitylogOptions method in both models: Define the getActivitylogOptions method in both the User and Contact models to specify which attributes should be logged.

  3. Set up the ResourceLogger for the Contact model: Ensure that the ResourceLogger is also set up for the Contact model to log its changes.

Here’s how you can do it:

User Model

use HasFactory, InteractsWithMedia, LogsActivity;

public function contacts()
{
    return $this->hasMany(Contact::class);
}

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

Contact Model

use HasFactory, LogsActivity;

public function user()
{
    return $this->belongsTo(User::class);
}

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

User Logger

public static function resource(ResourceLogger $logger): ResourceLogger
{
    return $logger
        ->preloadRelations('contacts')
        ->fields([
            Field::make('name')
                ->label(('Name')),
            Field::make('domain')
                ->label(('Domain')),
            Field::make('email')
                ->label(('Email')),
            Field::make('phone')
                ->label(('Phone')),

            Field::make('contacts')
                ->hasMany('contacts')
                ->table([
                    Field::make('name'),
                    Field::make('email'),
                    Field::make('phone'),
                ])
                ->label('Contacts'),
        ]);
}

Contact Logger

You need to set up a similar logger for the Contact model to ensure its changes are logged.

public static function resource(ResourceLogger $logger): ResourceLogger
{
    return $logger
        ->fields([
            Field::make('name')
                ->label(('Name')),
            Field::make('email')
                ->label(('Email')),
            Field::make('phone')
                ->label(('Phone')),
            Field::make('user_id')
                ->label(('User ID')),
        ]);
}

Additional Steps

  • Ensure Event Listeners are Set Up: Make sure that the event listeners for the created, updated, and deleted events are properly set up for both models.

  • Check Package Documentation: Refer to the noxoua/filament-activity-log documentation for any additional configuration or updates.

By following these steps, you should be able to log activities for both the User and Contact models, including changes in their relationships.

Please or to participate in this conversation.