richan-fongdasen/eloquent-blameable
blameable behavior (e.g., created_by, updated_by, deleted_by fields), aligning well with Laravel’s ORM-first architecture. It abstracts audit trail logic into reusable traits, reducing boilerplate in CRUD-heavy applications (e.g., SaaS platforms, admin panels).Auth facade for user resolution, avoiding tight coupling with business logic. Complements packages like spatie/laravel-activitylog for broader observability.Blameable) attaches to models; no middleware or service provider changes required. Works with Laravel 10+ (PHP 8.1+).boot() methods overriding blameable events.observers or model events managing created_at/updated_at.SoftDeletes) if deleted_by isn’t pre-defined.| Risk | Mitigation |
|---|---|
| Auth Resolution Failures | Validate auth()->user() exists before model operations; fallback to null. |
| Performance Overhead | Add indexes to *_by columns if queries filter by them. |
| Version Skew | Test with Laravel 11+ early; check for breaking changes in eloquent-blameable v2.0+. |
| Data Migration | Backfill *_by columns via seeder or data migration if adopting mid-project. |
spatie/laravel-medialibrary events) that could conflict?User model? Will blameable resolve correctly?*_by columns bloat queries? Plan for read replicas or caching (e.g., blameable in API responses).auth() in unit tests; verify deleted_by in soft-delete scenarios.laravel/breeze, laravel/jetstream, or custom providers.spatie/laravel-activitylog (for full audit trails).laravel/sanctum/passport to expose blame data in responses.Post, UserProfile).created_by/updated_by columns (rename if needed).use RichanFongdasen\EloquentBlameable\Blameable;
class Post extends Model { use Blameable; }
Schema::table('posts', function (Blueprint $table) {
$table->foreignId('created_by')->nullable()->constrained('users');
$table->foreignId('updated_by')->nullable()->constrained('users');
});
b. Backfill data:
Post::query()->update(['created_by' => auth()->id()]); // Hypothetical backfill
c. Apply trait to models.auth()->shouldUse() to simulate different users.deleted_by.deleted_by column exists before using Blameable.useTimestamps = false but may need manual created_by/updated_by handling.updated_by changes (e.g., Model::refresh()).LogEntry).created_by in responses).auth()->id() on model saves.service accounts) require one trait change.*_by columns to large tables may lock tables during backfills.blameable failures (e.g., auth()->user() null) in App\Exceptions\Handler.deleted_by: null) or resolve disputes via logs.*_by columns if frequently queried:
Schema::table('posts', function (Blueprint $table) {
$table->index('created_by');
});
save().auth()->setUser() is called before model operations.| Failure | Impact | Mitigation |
|---|---|---|
| Auth System Down | Blame fields set to null. |
Fallback to config('app.blame_fallback') or service account. |
| Database Constraints | Foreign key violations on *_by. |
Use nullable() or default to a "system" user. |
| Trait Overrides | Custom save() breaks blame logic. |
Extend trait or use protected $blameable = true. |
| Time Zone Issues | updated_at vs. updated_by confusion. |
Document that blame fields are user-specific, not time-based. |
Blameable and how to test.Post shows correct created_by").created_by (e.g., Post::where('created_by', auth()->id())).Blameable).null values in blame fields to identify auth gaps.How can I help you explore Laravel packages today?