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

Laravel Event Sourcing Laravel Package

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.

View on GitHub
Deep Wiki
Context7

title: Aggregate Partials weight: 11

Aggregate partials can be used to split large aggregate roots into separate classes. An aggregate partial belongs to an aggregate root, so from the outside you'd still interact with the aggregate root.

Just like aggregate roots, aggregate partials can record and apply events, these events are linked to the aggregate root a partial belongs to.

Here's an example of an aggregate partial, which manages cart items and is part of the cart aggregate root:

namespace Spatie\Shop\Cart\Partials;

use Spatie\EventSourcing\AggregateRoots\AggregatePartial;

class CartItems extends AggregatePartial
{
    private array $cartItems = [];

    public function isEmpty(): bool
    {
        return count($this->cartItems) === 0;
    }
    
    public function addItem(
        string $cartItemUuid,
        Product $product,
        int $amount
    ): self {
        $this->recordThat(new CartItemAdded(
            cartItemUuid: $cartItemUuid,
            productUuid: $product->uuid,
            amount: $amount,
        ));

        return $this;
    }

    protected function applyCartItemAdded(
        CartItemAdded $cartItemAdded
    ): void {
        $this->cartItems[$cartItemAdded->cartItemUuid] = null;
    }
}

The cart aggregate root, in its turn, links to its partials by adding them as protected properties:

class CartAggregateRoot extends AggregateRoot
{
    protected CartItems $cartItems;

    public function __construct()
    {
        $this->cartItems = new CartItems($this);
    }
}

The package will determine automatically that CartItems is a partial, and will dispatch events to it without any other configuration.

As said before, you'd still interact with the aggregate root from the outside, so CartAggregateRoot still needs a method to add an item, though all functionality related to it is moved to a separate class:

class CartAggregateRoot extends AggregateRoot
{
    // …
    
    public function addItem(
        string $cartItemUuid,
        Product $product,
        int $amount
    ): self {
        $this->cartItems->addItem(
            $cartItemUuid,
            $product,
            $amount,
        );

        return $this;
    }
}

Want to know more?

Our course, Event Sourcing in Laravel covers aggregate roots and partials in depth:

    1. Aggregate Roots
    1. State Management in Aggregate Roots
    1. Aggregate Partials
    1. State Machines with Aggregate Partials
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.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport