Laravel Multiple Model Events
<p>I'm trying to sync my database with an external service.</p>

<p>I'm using Algolia search in a couple of places across a web application.</p>

<p>It's indexed with a couple of models however I need it to re-index in the event that any changes are made to the database i.e. when multiple model events are fired.</p>

<p>My first approach was to action everything within the <strong>boot</strong> method of the <strong>AppServiceProvider</strong></p>

<pre><code>public function boot()
{
    $events = ['created', 'updated', 'deleted', 'restored'];

    // reindex handlers for models relevant to Algolia search
    foreach ($events as $evt) {
        Order::registerModelEvent($evt, function () {
            Order::reindex();
        });
        Product::registerModelEvent($evt, function () {
            Product::reindex();
            Product::setSettings();
        });
    }
}
</code></pre>

<p>This is my approach to avoid multiple conditionals using the standard model functions exampled in the <a href="https://laravel.com/docs/5.2/eloquent#events">docs</a>.</p>

<p>However I'm assuming there's a better way using Laravel Event Listeners.</p>

<pre><code>namespace App\Listeners;

class OrderEventListener
{
    // handlers

    public function subscribe($events)
    {
        $events-&gt;listen(
            // model events
        );
    }
}
</code></pre>

<p>Although I'm unsure how to tap into the model events in the <strong>listen</strong> method.</p>