foxen/laravel-model-activity-log
Automatically log Eloquent model activity in Laravel: create, update (with attribute diffs), delete, and restore (SoftDeletes). Supports per-model log names, ignoring or redacting attributes, tracks the authenticated user or falls back to “System”, and can prune old entries.
Pros:
created, updated, deleted, restored) to log activity without manual intervention, aligning well with Laravel’s Eloquent ecosystem.LogsActivity) for model integration, reducing boilerplate and maintaining separation of concerns.restored events if the model uses SoftDeletes.old_values and new_values for updates), useful for compliance or debugging.Cons:
activity_log table, which may not scale for complex queries (e.g., multi-model joins, aggregations).updated event (with old_values/new_values) could introduce overhead for high-frequency models.activity_log) with a fixed schema, requiring migration and potential index tuning (e.g., for causer_id, model_type, or action).causer_id). Systems without auth may need customization.activity_log table (e.g., adding columns) may require backward-compatible migrations.activity_log table during peak loads.Use Case Alignment:
Performance:
updated events with old_values/new_values impact performance?activity_log table?Authentication:
causer_id always be populated (e.g., for API calls or background jobs)?Data Retention:
Extensibility:
logs table or third-party tools like Sentry)?Alternatives:
Auth::user()). Custom logic may be needed for non-standard auth flows (e.g., API tokens, service accounts).assertDatabaseHas for log verification).Dependency Update:
composer.json and run composer update.Configuration:
php artisan vendor:publish --provider="Foxen\LaravelModelActivityLog\Providers\ActivityLogServiceProvider" --tag="config".foxen_activitylog.php (e.g., TTL settings, excluded models/actions).Database Migration:
php artisan migrate to create the activity_log table.Schema::table('activity_log', function (Blueprint $table) {
$table->index(['model_type', 'model_id']);
$table->index(['causer_id']);
$table->index(['action']);
$table->index(['created_at']);
});
Model Integration:
LogsActivity trait to target models:
use Foxen\LaravelModelActivityLog\Traits\LogsActivity;
class Post extends Model {
use LogsActivity;
// ...
}
'excluded_models' => [
'App\Models\UnimportantModel',
],
'excluded_actions' => [
'forceDelete',
],
Pruning Setup:
// app/Console/Kernel.php
protected function schedule(Schedule $schedule) {
$schedule->command('foxen:activity-log:prune')->daily();
}
Testing:
boot() methods or observers don’t interfere with the trait’s event listeners.Phase 1: Pilot Models
Post, UserProfile) to test performance and log accuracy.Phase 2: Core Models
Order, Payment) with monitoring for performance impact.Phase 3: Full Rollout
Phase 4: Optimization
foxen_activitylog.php settings (e.g., TTL, exclusions) to avoid per-environment inconsistencies.How can I help you explore Laravel packages today?