spatie/laravel-event-sourcing
Event sourcing toolkit for Laravel: build aggregates, projectors, and reactors to store state changes as events. Ideal for audit trails, decisions based on history, and future reporting needs. Includes docs, examples, and an optional course.
spatie/laravel-event-sourcing-eloquent) or custom storage backends (e.g., PostgreSQL JSONB, Redis).EventServiceProvider, Queue).Order) to validate the pattern.OrderUpdated).OrderProjection).UserRegistered event but keep users table for legacy APIs.events, projections) must be managed carefully.Illuminate\Events).composer require spatie/laravel-event-sourcing.php artisan vendor:publish --provider="Spatie\EventSourcing\EventSourcingServiceProvider".use Spatie\EventSourcing\AggregateRoot;
class Order extends AggregateRoot
{
public function place(OrderPlaced $event)
{
$this->recordThat($event);
// Business logic
}
}
use Spatie\EventSourcing\Projection;
class OrderProjection extends Projection
{
public function whenOrderPlaced(OrderPlaced $event)
{
// Update materialized view
}
}
use Spatie\EventSourcing\Reactor;
class SendWelcomeEmailReactor extends Reactor
{
public function handle(OrderPlaced $event)
{
Mail::to($event->user)->send(new WelcomeEmail());
}
}
EventSourcing::fake() for unit tests.Projection::refresh().EventSourcing::getRecordedEvents() to inspect aggregate history.Projection::refresh() to rebuild materialized views.orders_events, users_events).| Failure Type | Impact | Mitigation |
|---|---|---|
| Event Store Corruption | Lost events, inconsistent state. | Backups, transactional writes. |
| Projection Failure | Stale read models. | Retry mechanisms, health checks. |
| Reactor Crash | Missed side effects (e.g., emails). | Dead-letter queues, exponential backoff. |
| Concurrency Issues | Race conditions in aggregates. | Optimistic locking, event versioning. |
| Database Downtime | Unavailable event store. | Multi-region replication, async fallback. |
How can I help you explore Laravel packages today?