Skip to content
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

Feature/Allow to set custom StateChanged event #237

Merged
merged 2 commits into from
Sep 25, 2023

Conversation

fmeccanici
Copy link

This PR add the functionality of registering a custom StateChanged event. I stumbled upon this myself on a project for a client, where I wanted to listen to a specific state change and apply logic for this specific state.

Since only the StateChanged event is fired, I have to add all the listeners to this event and then place if statements to check if the model is equal to the model I want the logic to apply to.

Example
In the event service provider I register StateXChangedListener to the StateChanged event. Right now I have to do this in the listener:

if ($event->model::class === ModelWithStateX::class) {
   // Apply logic for StateX state change
}

Another way is to use the updating event on the model. But this is also not ideal as I don't know which state transition has taken place.

I also saw that there is an open discussion about custom events (see #215). I am not sure if this is what they mean by customised state changed event, but for me this implementation would be very useful.

@freekmurze freekmurze merged commit 63e27e2 into spatie:main Sep 25, 2023
@freekmurze
Copy link
Member

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants