Skip to content

Basic usage

The default mode is an interactive command line. In this example, uuid is used as the primary key, with an aggregate class.

Terminal window
php artisan make:event-sourcing-domain Animal
Which is the name of the domain? [Animal]
> Animal
Do you want to import properties from existing database migration?
> no
Do you want to specify model properties?
> yes
Property name (exit to quit)?
> name
Property type? (e.g. string, int, boolean. Nullable is accepted, e.g. ?string)
> string
Property name (exit to quit)?
> age
Property type? (e.g. string, int, boolean. Nullable is accepted, e.g. ?string)
> int
Property name (exit to quit)?
> exit
Do you want to use uuid as model primary key?
> yes
Do you want to create an Aggregate class?
> yes
Do you want to create a Reactor class?
> yes
Your choices:
| Option | Choice |
|----------------------------|-------------|
| Model | Animal |
| Domain | Animal |
| Namespace | Domain |
| Use migration | no |
| Primary key | uuid |
| Create Aggregate class | yes |
| Create Reactor class | yes |
| Create PHPUnit tests | no |
| Create failed events | no |
| Model properties | string name |
| | int age |
| Notifications | no |
Do you confirm the generation of the domain?
> yes
Domain [Animal] with model [Animal] created successfully.

Directory structure generated (using uuid as primary key):

app/
├── Domain/
│ └── Animal/
│ ├── Actions/
│ │ ├── CreateAnimal
│ │ ├── DeleteAnimal
│ │ └── UpdateAnimal
│ ├── Aggregates/
│ │ └── AnimalAggregate
│ ├── DataTransferObjects/
│ │ └── AnimalData
│ ├── Events/
│ │ ├── AnimalCreated
│ │ ├── AnimalDeleted
│ │ └── AnimalUpdated
│ ├── Projections/
│ │ └── Animal
│ ├── Projectors/
│ │ └── AnimalProjector
│ └── Reactors/
│ └── AnimalReactor
└── etc.

If Spatie event sourcing is configured to auto-discover projectors, the following code is immediately usable:

use App\Domain\Animal\Actions\CreateAnimal;
use App\Domain\Animal\DataTransferObjects\AnimalData;
use App\Domain\Animal\Projections\Animal;
// This will create a record in 'animals' table, using projector AnimalProjector
(new CreateAnimal)(new AnimalData(
name: 'tiger',
age: 7
));
// Retrieve record
$animal = Animal::query()->where('name', 'tiger')->first();