Capture the individual Eloquent Model event in Laravel 5.4 -
by default, laravel eloquent model fires following events : 'creating', 'created', 'updating', 'updated', 'deleting', 'deleted', 'saving', 'saved', 'restoring', 'restored'
i using listing eloquent events using wildcard listener in appserviceprovider
:
/** * bootstrap application services. * * @return void */ public function boot() { \event::listen(['eloquent.*'], function ($a){ var_dump($a); }); }
i getting nothing when dump $a
.
i know can capture events observable. there other ways this?
this might little bit late answer question , prefer perform using event subscriber.
you can have on it:
you need create event subscriber this:
usereventsubscriber.php
namespace app\listeners; class usereventsubscriber { /** * handle user created events. */ public function onusercreated($event) { //perform want after user created. } /** * register listeners subscriber. * * @param illuminate\events\dispatcher $events */ public function subscribe($events) { $events->listen( 'eloquent.created: app\users', 'app\listeners\usereventsubscriber@onusercreated' ); } }
now register event listener in eventserviceprovider
, listens every user created event fired eloquent.
eventserviceprovider.php
namespace app\providers; use illuminate\foundation\support\providers\eventserviceprovider serviceprovider; class eventserviceprovider extends serviceprovider { /** * event listener mappings application. * * @var array */ protected $listen = [ // ]; /** * subscriber classes register. * * @var array */ protected $subscribe = [ 'app\listeners\usereventsubscriber', ]; }
in view little bit cleaner way don't need touch serviceprovider
if want add multiple events related user.
hope helps you.
Comments
Post a Comment