-
Notifications
You must be signed in to change notification settings - Fork 11k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[12.x] Enable listening to multiple Eloquent events #53562
base: master
Are you sure you want to change the base?
[12.x] Enable listening to multiple Eloquent events #53562
Conversation
This looks like it'll clean up alot of my code base! |
hey @istiak-tridip is there a use case for listening on all events? |
@chr15k One of the goals of this PR was to align with
Happy to hear your thoughts! |
@istiak-tridip thanks, just to be more specific; my understanding is in order to achieve this with Event::listen('*', function () {
Cache::forget('categories');
}); I don't believe that you can pass a callback as first param with for example, this will not work: Event::listen(function () {
Cache::forget('categories');
}); But this will: Event::listen(function (CategoryDeletedEvent $event) {
Cache::forget('categories');
}); However, the following does work, which IMO is unexpected if going by the above behaviour, and the underlying '*' wildcard that's in effect, is hidden from the dev: Categories::listen(function () {
Cache::forget('categories');
}); Let me know your thoughts. Cheers! |
@chr15k Huh, I can't believe I missed that. Thanks for pointing it out! 🙌 What syntax would you suggest? Based on my current implementation, developers can still use With that in mind, I propose keeping |
@istiak-tridip yeh I'd like to hear others' opinions on this for sure, as I can see this as something I'd use in my projects and agree it would be quite useful to add a catch-all for events on a model. I suppose all I'm wondering is if the wildcard should be set explicitly or not, for the purpose of clearer intent. If so, a straightforward approach could be to disallow the first parameter from accepting a closure. |
Description
This PR introduces a new
Model::listen
method, allowing developers to listen to multiple Eloquent events at once.This enhancement improves the developer experience and aligns with the existing
Event::listen
behavior. Additionally, it introduces support for wildcard event listening for Eloquent models.Currently, performing the same action for multiple events looks like this:
With This PR:
Targeting the
master
branch based on @taylorotwell's suggestion.