Skip to content
This repository has been archived by the owner on Sep 20, 2019. It is now read-only.

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
Rias committed Sep 5, 2019
1 parent 68424c0 commit 5faecfa
Showing 1 changed file with 17 additions and 8 deletions.
25 changes: 17 additions & 8 deletions docs/advanced-usage/storing-metadata.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,23 @@ You can add metadata, such as the `id` of the logged in user, to a stored event.

## Storing metadata on all events

If you need to store metadata on all events you can leverage Laravel's native models events.
If you need to store metadata on all events you can leverage Laravel's native models events when using the `EloquentStoredEventRepository`.

You must configure the package [use your own event storage model](/laravel-event-projector/v2/advanced-usage/using-your-own-event-storage-model). On that model you can hook into the model lifecycle hooks.
You must configure the package to [use your own event storage repository](/laravel-event-projector/v3/advanced-usage/using-your-own-event-storage-repository) that extends the `EloquentStoredEventRepository` with a custom Eloquent model. On that model you can hook into the model lifecycle hooks.

```php
use Spatie\EventProjector\Models\StoredEvent;
use Spatie\EventProjector\EloquentStoredEventRepository;

class CustomStoredEventRepository extends EloquentStoredEventRepository
{
protected $storedEventModel = CustomStoredEvent::class;
}
```

```php
use Spatie\EventProjector\Models\EloquentStoredEvent;

class CustomStoredEvent extends StoredEvent
class CustomStoredEvent extends EloquentStoredEvent
{
public static function boot()
{
Expand All @@ -29,7 +38,8 @@ class CustomStoredEvent extends StoredEvent

## Storing metadata via a projector

The `StoredEvent` instance will be passed on to any projector method that has a variable named `$storedEvent`. On that `StoredEvent` instance there is a property, `meta_data`, that returns an instance of `Spatie\SchemalessAttributes\SchemalessAttributes`.
The `StoredEvent` instance will be passed on to any projector method that has a variable named `$storedEvent`. You'll also need the `StoredEventRepository` that is used by the application to update the stored event.
On the `StoredEvent` instance there is a property, `meta_data`, that returns an array. You can update this array to store any metadata you like.

Here's an example:

Expand All @@ -53,13 +63,12 @@ class MetaDataProjector implements Projector
MoneyAdded::class => 'onMoneyAdded',
];

public function onMoneyAdded(StoredEvent $storedEvent)
public function onMoneyAdded(StoredEvent $storedEvent, StoredEventRepository $repository)
{

if (Projectionist::isReplaying()) {
$storedEvent->meta_data['user_id'] = auth()->user()->id;

$storedEvent->save();
$repository->update($storedEvent);
}

// ...
Expand Down

0 comments on commit 5faecfa

Please sign in to comment.