- Why should I use spatie/laravel-event-projector instead of spatie/laravel-event-sourcing?
- This package is ideal if you only need projection capabilities for read models or are starting with event sourcing basics. However, since it’s archived, consider upgrading to `laravel-event-sourcing` for active maintenance, broader features, and Laravel 10+ support. The upgrade path from v3 is straightforward.
- Does this package support Laravel 10?
- No, this package is officially supported up to Laravel 8.x. For Laravel 10, use `spatie/laravel-event-sourcing` instead, which includes modern PHP and Laravel compatibility. You may need patches or adjustments for Laravel 9+.
- How do I store events in the database?
- Events are stored in a dedicated `events` table by default, with columns for aggregate ID, event class, and payload (serialized as JSON). You can also store events in a JSON column within your aggregate table if preferred, though this may impact query performance.
- Can I use this package for audit logging?
- Yes, event sourcing inherently supports audit logging by storing every state change as an event. Projectors can then reconstruct the full history or generate reports. This is especially useful for compliance or debugging.
- How do I handle failed projections or duplicate events?
- Projectors are designed to be idempotent, meaning they can safely replay events without side effects. Failed projections can be logged or retried via Laravel’s queue system. For duplicates, use unique constraints on the `events` table or implement deduplication logic in projectors.
- What’s the best way to migrate an existing Laravel app to event sourcing?
- Start with a single aggregate (e.g., `Order`) and its projections. Backfill historical events from your existing data using a migration script. Test event replay thoroughly to ensure projections match the current state before rolling out incrementally.
- How do I optimize projection performance for large datasets?
- Use incremental updates instead of full rebuilds where possible. Index projection tables on frequently queried columns (e.g., `aggregate_id`, `timestamp`). For read-heavy workloads, consider materialized views or caching projected data.
- Is this package compatible with Laravel Queues for async processing?
- Yes, event dispatching and projection can be queued using Laravel’s queue system (e.g., Redis, database). This is useful for decoupling event processing from the main request flow and improving performance.
- What alternatives exist if I need full event sourcing support?
- For a more modern and actively maintained solution, use `spatie/laravel-event-sourcing`, which includes snapshots, event versioning, and better Laravel 10+ compatibility. Other alternatives include `prooph/event-store` or `axiomlabs/axiom` for advanced event sourcing needs.
- How do I test event sourcing logic in Laravel?
- Use PHPUnit to test aggregates by replaying events and asserting the final state. Mock projectors to verify they update read models correctly. Laravel’s testing helpers (e.g., `actingAs`, `assertDatabaseHas`) work seamlessly with this package.