0

I decided to follow various "best practice" recommendations I've seen and put Event::listen code in the Model::boot static method for the relevant model. This works perfectly for half a dozen of them, completely fails to execute for others.

All extend from the same base class (which in turn extends \Illuminate\Database\Eloquent\Model)

All use the public static function boot() method

All use the same \Event::listen('event',function(){}); format

It's not a problem with multiple listeners, I can put two in the same working Model and they both trigger, but none will every be triggered in the non-working ones.

There's not even any code to paste in, I'm using by-the-book (to the best of my knowledge) code,

class MyClass extends \Illuminate\Database\Eloquent\Model { public static function boot() { \Event::listen('my.event', function($arg1, $arg2){ \Log::debug("This may or may not trigger."); }); } } 

No Exceptions are thrown, no errors logged, it just doesn't run. I can c&p the code into one of the "working" Models and it will work exactly as expected.

Is there some obscure problem or nuance I'm missing? This inconsistency is maddening.

5
  • 2
    Are you actually using that model in your code? Laravel lazy auto-loads - so I dont think it will boot the model until it is needed? (I'm not 100% sure though) Commented Oct 23, 2014 at 6:04
  • Oh damn, that very well could be it, then. I have a huge pile of models, but there's no guarantee any given one will have been used already when an event it listens for fires. That's incredibly disappointing, as that would be the perfect way to compartmentalize the events. Commented Oct 23, 2014 at 6:46
  • 1
    So - hang on - are you trying to monitor Model Events inside your models. Or are you trying to monitor entire application events inside your models? It looks like your trying to do the later? Commented Oct 23, 2014 at 6:51
  • My idea was to listen for (for instance) user.joined.company in the latter Model, since in this case it would be the Company doing things based on the User's action. The now-obvious problem is that the Company model might not have been used anywhere and so wouldn't be listening. I'll just have to move the events into the actor's class instead. Commented Oct 23, 2014 at 6:57
  • Your first comment was the way-of-thought adjustment I needed to get on track, if you move that to an answer I'll accept it. Commented Oct 23, 2014 at 19:39

1 Answer 1

1

Are you actually using that model in your code? Laravel lazy auto-loads - so I dont think it will boot the model until it is needed?

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.