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:
-
Ensure the
LogsActivitytrait is used in both models: Make sure both theUserandContactmodels use theLogsActivitytrait. -
Configure the
getActivitylogOptionsmethod in both models: Define thegetActivitylogOptionsmethod in both theUserandContactmodels to specify which attributes should be logged. -
Set up the
ResourceLoggerfor theContactmodel: Ensure that theResourceLoggeris also set up for theContactmodel 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, anddeletedevents 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.