Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

greegus's avatar

Observing any Model inherited object events

What do you suggest the best way for bind an action performed on events triggered by all classes which are extending the abstract Model class in L5?

So far i've been only able to manually registering observer on each existing entity class in my app, but thats getting a little messy when i would like to push this to a package for easier usage on other projects. Its just a few extra lines of code, but i'm looking for a way how to avoid them anyway.

0 likes
1 reply
JarekTkaczyk's avatar
Level 53

Use wildcard events:

Event::listen('eloquent.creating: *', function ($model) {
   // do something with the $model
});

Event::listen('eloquent.created: *', function ($model) { ... });

and so on.

1 like

Please or to participate in this conversation.