Metadata
If you find yourself wanting to include some additional data on every event, Verbs makes it very easy to automatically include metadata.
In a ServiceProvider or Middleware call the following method:
1Verbs::createMetadataUsing(function (Metadata $metadata, Event $event) {2 $metadata->team_id = current_team_id();3});
You can call this method as many times as you would like. This is particularly useful for third-party packages, allowing them to add metadata automatically.
It's also possible to simply return an array (or Collection), and Verbs will merge that in for you:
1Verbs::createMetadataUsing(fn () => ['team_id' => current_team_id()]);
This is particularly useful for events where accompanying data is moreso about the events, and doesn't necessarily need to be a param in the event.
Accessing Metadata
You can use the metadata() method to get the metadata from the event. Use put() and get() for additional fluency:
1$event->metadata()->put('foo', 'bar');2 3$event->metadata('foo'); // 'bar'4$event->metadata()->get('foo'); // 'bar'
Toggling Metadata
Maybe you don't want every event to have metadata. Verbs makes it easy to opt out when you need to.
Here's an example of a user who prefers no promotional notifications:
1public function sendPromotionalNotification($user) 2{ 3 $user_preferences = $this->getUserPreferences($user->id); 4 5 Verbs::createMetadataUsing(fn (Metadata $metadata) => [ 6 'suppress_notifications' => !$userPreferences->acceptsPromotionalNotifications, 7 ]); 8 9 PromotionalEvent::fire(details: $user->location->promoDetails());10 11 // resets Metadata bool for the next user12 Verbs::createMetadataUsing(fn (Metadata $metadata) => ['suppress_notifications' => false]);13}
Then, where you handle your promotional event messages:
1public function handlePromotionalEvent(PromotionalEvent $event)2{3 if ($event->metadata('suppress_notifications', false)) {4 return;5 }6 7 $this->sendNotification($event->details);8}