Skip to content

Mailable collections

Mailable collections and registering your own mailables

To register your mailables: 1. Add a new class that inherits from \Domain\Core\Mails\AbstractMailableCollection class. 2. Override its abstract method called registerMailables to register your mailables. 3. Call static register method of your collection in your service provider.

For example:

<?php

namespace JohnIt\Bc\Shop\Domain\Mails;

use Domain\Core\Mails\AbstractMailableCollection;

class ShopMailableCollection extends AbstractMailableCollection
{
    public function registerMailables()
    {
        $this->push(
            OrderConfirmationMailable::class,
        );
    }
}
And then in your service provider add following code in register method:
public function register()
{
    ShopMailableCollection::register();
}

You can also use dependency injection to have an instance of you collection injected into method parameters:

public function myControllerMethod(ShopMailableCollection $mailables)
{
}

AbstractMailableCollection inherits from Laravel’s Collection class, so you can use all its methods.

If you want to get a list of all registered collections in a system use AbstractMailableCollection::getCollections() method. Also please, note that \Domain\Core\Mails\Mailable class implements \JohnIt\Bc\Core\Domain\Models\Defaultable interface, so you can instantiate a default (read demo) instance of any Mailable from collection using static default() method on class.