-
Notifications
You must be signed in to change notification settings - Fork 11.6k
[12.x] Fix support for adding custom observable events from traits #55286
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] Fix support for adding custom observable events from traits #55286
Conversation
- This ensure that all custom events have been registered by any traits first. - It also prevents a new instance of the model from being created by the call to `observe` too early, since an instance created before booting has finished would be in an inconsistent state due to not all of the trait boot methods being run yet and not all of the initializer methods would be registered yet, so they would not be run either.
|
There are some funky issues with the tests and listening for the |
- This removes any reliance on the event dispatcher, which complicated testing.
|
The complexity introduced to testing by using the A new |
06aad07 to
60731aa
Compare
|
@willrowe I'm not sure, but I guess this broke the functionality of defining magic methods on traits for Eloquent models, e.g., |
|
@azzazkhan how so? |
|
Leaving a comment in case this helps someone else with their Laravel 12 upgrade. This change makes sense and it solves another issue. But, when upgrading to Laravel 12, this change caused a change in our business logic. We expect the event hooks set by Previously, in our trait, we had protected static function bootMyTrait(): void
{
static::deleting(function ($model): void {
// logic here
});
}With this change, the above is now registered before our To fix this, I've used the new protected static function bootMyTrait(): void
{
static::whenBooted(
fn () => static::deleting(function ($model): void {
// logic here
})
);
}Hopefully this helps someone. |
|
@tom-on-the-internet |
|
On second thought, it's probably best to leave it as-is. It does not look like the boot order of traits is specified, so it shouldn't be relied upon. |
This delays the
observecall in the boot method for theHasEventstrait until after the model has finished booting so that any custom events registered by other traits are available to be matched up to observer class methods.This fixes the initial issue that was discussed in #53607