Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Filament Activity Log Laravel Package

pxlrbt/filament-activity-log

View on GitHub
Deep Wiki
Context7

Getting Started

First Steps

  1. Installation Run:

    composer require pxlrbt/filament-activity-log
    

    Publish the config (if needed):

    php artisan vendor:publish --provider="Pxlrbt\FilamentActivityLog\FilamentActivityLogServiceProvider"
    
  2. Basic Setup Ensure spatie/laravel-activitylog is installed (this package wraps it for Filament). Register the package in AppServiceProvider:

    use Pxlrbt\FilamentActivityLog\FilamentActivityLogServiceProvider;
    
    public function boot()
    {
        $this->app->register(FilamentActivityLogServiceProvider::class);
    }
    
  3. First Use Case Log an activity for a model (e.g., Post):

    use Spatie\Activitylog\LogOptions;
    use Spatie\Activitylog\Traits\LogsActivity;
    
    class Post extends Model
    {
        use LogsActivity;
    
        protected static $logAttributes = ['title', 'content'];
        protected static $logOnly = ['title'];
    }
    

    Access logs in Filament via the Activity Log widget or page.


Implementation Patterns

Common Workflows

  1. Model Logging

    • Use LogsActivity trait on Eloquent models.
    • Customize log attributes via $logAttributes, $logOnly, or $logExcept.
    • Example:
      class User extends Model
      {
          use LogsActivity;
      
          protected static $logAttributes = ['name', 'email'];
          protected static $logOnly = ['email'];
      }
      
  2. Manual Logging Trigger logs manually:

    use Spatie\Activitylog\Facades\Activity;
    
    Activity::log('custom_event', $user, ['custom_data' => 'value']);
    
  3. Filament Integration

    • Add the Activity Log widget to a dashboard:
      use Pxlrbt\FilamentActivityLog\Widgets\ActivityLogWidget;
      
      Dashboard::widget(ActivityLogWidget::class);
      
    • Create a dedicated Activity Log page:
      use Pxlrbt\FilamentActivityLog\Pages\ActivityLogPage;
      
      Route::get('/activity-log', ActivityLogPage::class)->name('filament.activity-log');
      
  4. Filtering & Search Use Filament’s built-in filters (e.g., by model, user, or event) via the UI. Customize query scopes in config/filament-activity-log.php:

    'query' => [
        'default' => fn ($query) => $query->where('properties->user_id', auth()->id()),
    ],
    
  5. Real-Time Updates Combine with Laravel Echo/Pusher for live log updates (extend the widget).


Integration Tips

  • Laravel Nova Users: Migrate existing Nova activity logs to Filament by syncing models.
  • Custom Events: Extend ActivityLog with custom events:
    Activity::performing('custom_event', function ($model) {
        return ['metadata' => $model->metadata];
    });
    
  • Soft Deletes: Ensure spatie/laravel-activitylog is configured for soft deletes if needed:
    'should_log_soft_deletes' => true,
    

Gotchas and Tips

Pitfalls

  1. Missing spatie/laravel-activitylog

    • Ensure the base package is installed (composer require spatie/laravel-activitylog).
    • Symptoms: Logs not appearing, errors about undefined classes.
  2. Permission Issues

    • Filament’s activity log page requires view activity logs permission by default.
    • Grant via:
      use Filament\Permissions\PermissionsPolicy;
      
      $policy = app(PermissionsPolicy::class);
      $policy->userCan('view activity logs', fn ($user) => $user->isAdmin());
      
  3. Performance with Large Logs

    • Avoid querying all logs at once. Use pagination:
      'perPage' => 20, // in config/filament-activity-log.php
      
    • Add database indexes to activity_logs table:
      php artisan activitylog:install
      
  4. Event Naming Conflicts

    • Default events (e.g., created, updated) may clash with custom events.
    • Rename custom events or use namespaces:
      Activity::log('user.updated_profile', $user);
      
  5. Model Not Logging

    • Verify $logAttributes is set and the model uses LogsActivity.
    • Check for typos in attribute names (case-sensitive).

Debugging

  1. Logs Not Appearing?

    • Check spatie/laravel-activitylog logs:
      tail -f storage/logs/laravel.log | grep activity
      
    • Test with a manual log:
      Activity::log('test_event', new User);
      
  2. Filament Widget Blank?

    • Clear Filament cache:
      php artisan filament:cache-reset
      
    • Check for JavaScript errors in browser console.
  3. Custom Data Not Saved

    • Ensure properties are serialized correctly:
      Activity::log('event', $model, ['key' => 'value']);
      
    • Verify config/activitylog.php properties_to_log includes your keys.

Extension Points

  1. Customize Log Display Override the widget’s table columns:

    use Pxlrbt\FilamentActivityLog\Widgets\ActivityLogWidget;
    
    ActivityLogWidget::make()
        ->table([
            'id',
            'log_name',
            'properties->custom_field',
        ]);
    
  2. Add Actions to Logs Extend the ActivityLogPage to add buttons (e.g., "Revert"):

    use Filament\Tables\Table;
    
    public function table(Table $table): Table
    {
        return $table
            ->columns([...])
            ->actions([
                Tables\Actions\Action::make('revert')
                    ->action(fn ($record) => /* logic */),
            ]);
    }
    
  3. Export Logs Use Filament’s export method:

    public static function getPages(): array
    {
        return [
            'export' => ActivityLogPage::class,
        ];
    }
    
  4. Webhook Notifications Trigger events on log creation:

    Activity::performing('*', function ($model) {
        event(new ActivityLogged($model));
    });
    
  5. Multi-Tenant Logs Scope logs to tenant ID:

    'query' => [
        'default' => fn ($query) => $query->where('properties->tenant_id', tenant()->id),
    ],
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hamzi/corewatch
minionfactory/raw-hydrator
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope