PHPackages                             nwidart/laravel-broadway - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. nwidart/laravel-broadway

ActiveLibrary[Framework](/categories/framework)

nwidart/laravel-broadway
========================

A Laravel adapter for the Broadway ES/CQRS package.

2.0.2(7y ago)12315.0k28[4 issues](https://github.com/nWidart/Laravel-broadway/issues)[1 PRs](https://github.com/nWidart/Laravel-broadway/pulls)MITPHPPHP &gt;=5.5.9

Since Jan 19Pushed 3y ago8 watchersCompare

[ Source](https://github.com/nWidart/Laravel-broadway)[ Packagist](https://packagist.org/packages/nwidart/laravel-broadway)[ RSS](/packages/nwidart-laravel-broadway/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (8)Dependencies (5)Versions (10)Used By (0)

Laravel Broadway
================

[](#laravel-broadway)

[![SensioLabsInsight](https://camo.githubusercontent.com/2110d651a041b8cd4a4cdd72f35fc52f6b76cd8a766c3319d36da0e1ecd37b8b/68747470733a2f2f696e73696768742e73656e73696f6c6162732e636f6d2f70726f6a656374732f30393263316564322d323461322d346161632d626639312d3832326463396466393865342f6d696e692e706e67)](https://insight.sensiolabs.com/projects/092c1ed2-24a2-4aac-bf91-822dc9df98e4)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/9947a1edba4119200f675bbaf0e2356cf2d54edb761a28949bae612195e83b4f/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f6e5769646172742f4c61726176656c2d62726f61647761792f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/nWidart/Laravel-broadway/?branch=master)[![Latest Stable Version](https://camo.githubusercontent.com/90e89721e1e3f5cfd83e0490bf6973cf989c54ae9ccd4b3887c06c6444262399/68747470733a2f2f706f7365722e707567782e6f72672f6e7769646172742f6c61726176656c2d62726f61647761792f76657273696f6e2e737667)](https://packagist.org/packages/nwidart/laravel-broadway)[![Total Downloads](https://camo.githubusercontent.com/54aabf514d902a953edf1bdb1d83ff78ed53c1b327d8dc6e0b8b2108b51416cb/68747470733a2f2f706f7365722e707567782e6f72672f6e7769646172742f6c61726176656c2d62726f61647761792f646f776e6c6f6164732e737667)](https://packagist.org/packages/nwidart/laravel-broadway)[![Latest Unstable Version](https://camo.githubusercontent.com/69dfeaa86d340ff31937e458577d460c3312f1df094d88a1030c5e4d6fb44d58/68747470733a2f2f706f7365722e707567782e6f72672f6e7769646172742f6c61726176656c2d62726f61647761792f762f756e737461626c652e737667)](//packagist.org/packages/nwidart/laravel-broadway)[![License](https://camo.githubusercontent.com/217d95895df543f117541a1312b062bfcd85c79bd1a6e73f0c517535f334a368/68747470733a2f2f706f7365722e707567782e6f72672f6e7769646172742f6c61726176656c2d62726f61647761792f6c6963656e73652e737667)](https://packagist.org/packages/nwidart/laravel-broadway)

Laravel Broadway is an adapter for the [Broadway](https://github.com/qandidate-labs/broadway/) package.

It binds all needed interfaces for Broadway.

For reference, I've built a [demo laravel application](https://github.com/nWidart/Laravel-Broadway-Demo) that uses this package and some event sourcing techniques.

**Laravel 5 compatible package**

Laravel versionPackage version~4.2~0.2~5.0~0.3~5.1+~2.0Installation
------------

[](#installation)

### Install via composer

[](#install-via-composer)

```
composer require nwidart/laravel-broadway=~1.0

```

### Service Providers

[](#service-providers)

#### Laravel 5.5 (Auto discovery)

[](#laravel-55-auto-discovery)

If you are using Laravel 5.5, this package provides auto discovery, meaning you can start coding – the Global Service Provider is already added. In case you need just some Service Providers, please add the following section to your applications composer.json:

```
 "extra": {
    "laravel": {
      "dont-discover": [
        "nWidart/Laravel-broadway"
      ]
    }
  }

```

#### Laravel 5.4

[](#laravel-54)

To finish the installation you need to add the service providers.

You have a choice here, you can either use the main Service Provider which will load the following:

- [CommandBus](https://github.com/nWidart/Laravel-broadway/blob/master/src/Broadway/CommandServiceProvider.php)
- [EventBus](https://github.com/nWidart/Laravel-broadway/blob/master/src/Broadway/EventServiceProvider.php)
- [Serializers](https://github.com/nWidart/Laravel-broadway/blob/master/src/Broadway/SerializersServiceProvider.php)
- [EventStorage](https://github.com/nWidart/Laravel-broadway/blob/master/src/Broadway/EventStorageServiceProvider.php)
- [ReadModel](https://github.com/nWidart/Laravel-broadway/blob/master/src/Broadway/ReadModelServiceProvider.php)
- [MetadataEnricher](https://github.com/nWidart/Laravel-broadway/blob/master/src/Broadway/MetadataEnricherServiceProvider.php)
- [Support](https://github.com/nWidart/Laravel-broadway/blob/master/src/Broadway/SupportServiceProvider.php) (UuidGenerators,...)

Or choose to use only the Service providers you need. Don't know what you need ? Use the Global Service Provider provided.

#### Global Service Provider

[](#global-service-provider)

```
   Nwidart\LaravelBroadway\LaravelBroadwayServiceProvider::class
```

#### Separate Service Providers

[](#separate-service-providers)

- CommandBus

    ```
    Nwidart\LaravelBroadway\Broadway\CommandServiceProvider::class
    ```
- EventBus

    ```
    Nwidart\LaravelBroadway\Broadway\EventServiceProvider::class
    ```
- Serializers

    ```
    Nwidart\LaravelBroadway\Broadway\SerializersServiceProvider::class
    ```
- EventStorage

    ```
    Nwidart\LaravelBroadway\Broadway\EventStorageServiceProvider::class
    ```
- ReadModel

    ```
    Nwidart\LaravelBroadway\Broadway\ReadModelServiceProvider::class
    ```
- MetadataEnricher

    ```
    Nwidart\LaravelBroadway\Broadway\MetadataEnricherServiceProvider::class
    ```
- Support

    ```
    Nwidart\LaravelBroadway\Broadway\SupportServiceProvider::class
    ```

### (Optional) Publish configuration file and migration

[](#optional-publish-configuration-file-and-migration)

```
php artisan vendor:publish

```

This will publish a `config/broadway.php` file and a `database/migrations/create_event_store_table.php` file.

### (Optional) Run migration

[](#optional-run-migration)

Last step, run the migration that was published in the last step to create the event\_store table.

If you haven't published the vendor files, you can use the command explained below:

```
php artisan broadway:event-store:migrate table_name

```

Configuration
-------------

[](#configuration)

### Event Store

[](#event-store)

To create the event store you can call the following command:

```
php artisan broadway:event-store:migrate table_name

```

In the configuration file, you can choose which driver to use as an event store and which connection to use.

```
'event-store' => [
    'table' => 'event_store',
    'driver' => 'dbal',
    'connection' => 'default',
],
```

Once done, you can bind your **EventStoreRepositories** in a Service Provider like so:

```
$this->app->bind(\Modules\Parts\Repositories\EventStorePartRepository::class, function ($app) {
    $eventStore = $app[\Broadway\EventStore\EventStore::class];
    $eventBus = $app[\Broadway\EventHandling\EventBus::class];
    return new MysqlEventStorePartRepository($eventStore, $eventBus);
});
```

For an in memory Event Store, all you need to do is change the driver in the configuration file and probably add a new event store repository implementation with an adequate name.

### Read Model

[](#read-model)

To set a read model in your application you first need to set the wanted read model in the package configuration.

Once that's done you can bind your **ReadModelRepositories** in a Service Provider like so:

```
$this->app->bind(\Modules\Parts\Repositories\ReadModelPartRepository::class, function ($app) {
    $serializer = $app[\Broadway\Serializer\Serializer::class];
    return new ElasticSearchReadModelPartRepository($app['Elasticsearch'], $serializer);
});
```

For an In Memory read model as an example:

```
$this->app->bind(\Modules\Parts\Repositories\ReadModelPartRepository::class, function ($app) {
    $serializer = $app[\Broadway\Serializer\Serializer::class];
    return new InMemoryReadModelPartRepository($app['Inmemory'], $serializer);
});
```

See the [demo laravel application](https://github.com/nWidart/Laravel-Broadway-Demo) and specifically the [Service Provider](https://github.com/nWidart/Laravel-Broadway-Demo/blob/master/Modules/Parts/PartServiceProvider.php) for a working example.

Registering subscribers
-----------------------

[](#registering-subscribers)

### Command handlers

[](#command-handlers)

To let broadway know which handlers are available you need to bind in the Laravel IoC container a key named `broadway.command-subscribers` as a singleton.

It's important to know Command Handlers classes in broadway need to get a Event Store repository injected.

Now just pass either an array of command handlers to the `laravelbroadway.command.registry` key out the IoC Container or just one class, like so:

```
$partCommandHandler = new PartCommandHandler($this->app[\Modules\Parts\Repositories\EventStorePartRepository::class]);
$someOtherCommandHandler = new SomeOtherCommandHandler($this->app[\Modules\Things\Repositories\EventStoreSomeRepository::class]);

$this->app['laravelbroadway.command.registry']->subscribe([
    $partCommandHandler,
    $someOtherCommandHandler
]);

// OR

$this->app['laravelbroadway.command.registry']->subscribe($partCommandHandler);
$this->app['laravelbroadway.command.registry']->subscribe($someOtherCommandHandler);
```

### Event subscribers

[](#event-subscribers)

This is pretty much the same as the command handlers, except that the event subscriber (or listener) needs an Read Model repository.

Example:

```
$partsThatWereManfacturedProjector = new PartsThatWereManufacturedProjector($this->app[\Modules\Parts\Repositories\ReadModelPartRepository::class]);
$someOtherProjector = new SomeOtherProjector($this->app[\Modules\Things\Repositories\ReadModelSomeRepository::class]);

$this->app['laravelbroadway.event.registry']->subscribe([
    $partsThatWereManfacturedProjector,
    $someOtherProjector
]);

// OR

$this->app['laravelbroadway.event.registry']->subscribe($partsThatWereManfacturedProjector);
$this->app['laravelbroadway.event.registry']->subscribe($someOtherProjector);
```

### Metadata Enricher

[](#metadata-enricher)

Broadways event store table comes with a field called "metadata". Here we can store all kind of stuff which should be saved together with the particular event, but which is does not fit to the domain aka the payload.

For example you like to store the ID of the current logged-in user or the IP or ...

Broadway uses Decorators to manipulate the event stream. A decorator consumes one or more Enrichers, which provide the actual data (user ID, IP). Right before saving the event to the stream, the decorator will loop through the registered enrichers and apply the data.

The following example assumes you added the global ServiceProvider of this package or at least the `Nwidart\LaravelBroadway\Broadway\MetadataEnricherServiceProvider`.

First we create the enricher. In this example lets assume we are interested in the logged-in user. The enricher will add the user ID to the metadata and returns the modified metadata object. However, in some cases – like in Unit Tests - there is no logged-in user available. To tackle this, the user ID can injected via constructor.

```
// CreatorEnricher.php

class CreatorEnricher implements MetadataEnricher
{
    /** @var int $creatorId */
    private $creatorId;

    /**
     * The constructor
     *
     * @param int $creatorId
     */
    public function __construct($creatorId = null)
    {
        $this->creatorId = $creatorId;
    }

    /**
     * @param Metadata $metadata
     * @return Metadata
     */
    public function enrich(Metadata $metadata)
    {
        if ($this->creatorId !== null) {
            $id = $this->creatorId;
        } else {
            $id = Auth::user()->id;
        }

        return $metadata->merge(Metadata::kv('createorId', $id));
    }
}
```

Second you need to register the Enricher to the decorator and pass the decorator to your repository

```
// YourServiceProvider.php

/**
 * Register the Metadata enrichers
 */
private function registerEnrichers()
{
    $enricher = new CreatorEnricher();
    $this->app['laravelbroadway.enricher.registry']->subscribe([$enricher]);
}

$this->app->bind(\Modules\Parts\Repositories\EventStorePartRepository::class, function ($app) {
    $eventStore = $app[\Broadway\EventStore\EventStore::class];
    $eventBus = $app[\Broadway\EventHandling\EventBus::class];

    $this->registerEnrichers();

    return new MysqlEventStorePartRepository($eventStore, $eventBus, $app[Connection::class], [$app[EventStreamDecorator::class]);
});
```

To retrieve the metadata you need to pass the DomainMessage as the 2nd parameter to an apply\*-method in your projector.

```
// PartsThatWhereCreatedProjector.php

public function applyPartWasRenamedEvent(PartWasRenamedEvent $event, DomainMessage $domainMessage)
{
	$metaData = $domainMessage->getMetadata()->serialize();
	$creator = User::find($metaData['creatorId']);

	// Do something with the user
}
```

All the rest are conventions from the Broadway package.

---

[Changelog](/CHANGELOG.md)
--------------------------

[](#changelog)

[License (MIT)](/LICENSE.md)
----------------------------

[](#license-mit)

###  Health Score

38

—

LowBetter than 85% of packages

Maintenance19

Infrequent updates — may be unmaintained

Popularity37

Limited adoption so far

Community22

Small or concentrated contributor base

Maturity64

Established project with proven stability

 Bus Factor1

Top contributor holds 75.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~200 days

Recently: every ~347 days

Total

8

Last Release

2728d ago

Major Versions

0.3.0 → 1.0.02017-02-01

1.0.0 → 2.0.02017-08-22

PHP version history (2 changes)0.1PHP &gt;=5.4.0

1.0.0PHP &gt;=5.5.9

### Community

Maintainers

![](https://www.gravatar.com/avatar/565946b8d70f5fa172a21f87b7f0e3abdce0611cc1cd300ec8e7b360bdb0a4bc?d=identicon)[nWidart](/maintainers/nWidart)

---

Top Contributors

[![nWidart](https://avatars.githubusercontent.com/u/882397?v=4)](https://github.com/nWidart "nWidart (133 commits)")[![LavaToaster](https://avatars.githubusercontent.com/u/838613?v=4)](https://github.com/LavaToaster "LavaToaster (31 commits)")[![Konafets](https://avatars.githubusercontent.com/u/363363?v=4)](https://github.com/Konafets "Konafets (6 commits)")[![AlessandroMinoccheri](https://avatars.githubusercontent.com/u/3356506?v=4)](https://github.com/AlessandroMinoccheri "AlessandroMinoccheri (3 commits)")[![moleculezz](https://avatars.githubusercontent.com/u/118499?v=4)](https://github.com/moleculezz "moleculezz (2 commits)")[![NoelDavies](https://avatars.githubusercontent.com/u/572911?v=4)](https://github.com/NoelDavies "NoelDavies (1 commits)")[![Big-Shark](https://avatars.githubusercontent.com/u/646054?v=4)](https://github.com/Big-Shark "Big-Shark (1 commits)")

---

Tags

laravelDomain Driven Designdddevent sourcingcqrsesbroadway

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/nwidart-laravel-broadway/health.svg)

```
[![Health](https://phpackages.com/badges/nwidart-laravel-broadway/health.svg)](https://phpackages.com/packages/nwidart-laravel-broadway)
```

###  Alternatives

[broadway/broadway

Infrastructure and testing helpers for creating CQRS and event sourced applications.

1.5k2.0M53](/packages/broadway-broadway)[broadway/broadway-bundle

Symfony bundle for broadway/broadway.

68853.8k7](/packages/broadway-broadway-bundle)[lunarstorm/laravel-ddd

A Laravel toolkit for Domain Driven Design patterns

17959.0k](/packages/lunarstorm-laravel-ddd)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
