andybeak
8/19/2016 - 12:25 PM

Laravel 5.2 model events

Laravel 5.2 model events

<?php

namespace App\Events\Handlers;

use Log;

class TransactionEventHandler
{

    /**
     * Handle the created event
     *
     * @access public
     */
    public function created()
    {
        Log::debug(__METHOD__ . ' :  bof');
    }

    /**
     * Handle the deleted event
     *
     * @access public
     */
    public function deleted()
    {
        Log::error(__METHOD__ . ' : Transactions should never be deleted!');
    }

    /**
     * @param $events
     */
    public function subscribe($events)
    {
        $events->listen('transaction.created',
            'App\Events\Handlers\TransactionEventHandler@created');

        $events->listen('transaction.updated',
            'App\Events\Handlers\TransactionEventHandler@updated');

        $events->listen('transaction.deleted',
            'App\Events\Handlers\TransactionEventHandler@deleted');
    }

    /**
     * Handle the updated event
     *
     * @access public
     */
    public function updated()
    {
        Log::debug(__METHOD__ . ' :  bof');
    }

}
<?php namespace App\Lib\Domain\Models;

use App\Lib\Traits\ModelEventThrower;
use Illuminate\Database\Eloquent\Model;

class Transaction extends Model
{

    use ModelEventThrower;

    /**
     * What events this model must throw events for
     * @var array
     */
    protected static $triggerEvents = ['created', 'updated', 'deleted'];
    
    // rest of model
}
<?php

namespace App\Lib\Traits;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Event;

/**
 * Class ModelEventThrower
 *
 * @package App\Traits
 *  Automatically throw Add, Update, Delete events of Model.
 */
trait ModelEventThrower
{

    /**
     * Automatically boot with Model, and register Events handler.
     */
    protected static function bootModelEventThrower()
    {
        foreach (static::getModelEvents() as $eventName) {
            static::$eventName(function (Model $model) use ($eventName) {
                try {
                    $reflect = new \ReflectionClass($model);
                    Event::fire(strtolower($reflect->getShortName()) . '.' . $eventName, $model);
                } catch (\Exception $e) {
                    return true;
                }
            });
        }
    }

    /**
     * Set the default events to act on if the $triggerEvents property does not exist on the model.
     *
     * @return array
     */
    protected static function getModelEvents()
    {
        if (isset(static::$recordEvents)) {

            return static::$recordEvents;

        }

        return [
            'created',
            'updated',
            'deleted',
        ];
    }
}
<?php

namespace App\Providers;

use Illuminate\Contracts\Events\Dispatcher as DispatcherContract;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

class EventServiceProvider extends ServiceProvider
{
    /**
     * The event listener mappings for the application.
     *
     * @var array
     */
    protected $listen = [
        'App\Events\PurlVisit' => [
            'App\Listeners\PurlVisitEventListener',
        ],
    ];

    /**
     * Register any other events for your application.
     *
     * @param  \Illuminate\Contracts\Events\Dispatcher  $events
     * @return void
     */
    public function boot(DispatcherContract $events)
    {
        parent::boot($events);

        $events->subscribe('App\Events\Handlers\TransactionEventHandler');
    }
}