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

Model bootTraits executes same method multiple times if two traits have the same base name #24548

Closed
Gummibeer opened this issue Jun 10, 2018 · 3 comments

Comments

@Gummibeer
Copy link
Contributor

Gummibeer commented Jun 10, 2018

  • Laravel Version: 5.6
  • PHP Version: 7.1
  • Database Driver & Version: MySQL

It's independent of versions so far I know.

Description:

The Illuminate\Database\Eloquent\Model::bootTraits() method executes the same bootXyzTrait method multiple times if the model uses two (or more) traits with the same base name.
This is simply resolvable by renaming the second trait but isn't cool if you want to extend a package trait for example.

Steps To Reproduce:

spatie/laravel-activitylog#392

Create a model (App\User) with a trait (App\Traits\HasEmail) which uses a trait (Developer\Package\Traits\HasEmail) with a trait boot method (bootHasEmail).

class App\User {
  use App\Traits\HasEmail;
}

trait App\Traits\HasEmail {
  use Developer\Package\Traits\HasEmail;
}

trait Developer\Package\Traits\HasEmail {
  protected static function bootHasEmail() {
    var_dump('booting:HasEmail');
  }
}

// will output:
// booting:HasEmail
// booting:HasEmail

If the trait adds oberserver or other logic that should run just once it will run these things two times.

@tillkruss
Copy link
Contributor

Can you submit a PR to resolve this?

@themsaid
Copy link
Member

PR with a fix submitted to #24556, thanks for sharing.

@Gummibeer
Copy link
Contributor Author

Thanks for this fix. Will this get a backport to older versions?
And thanks for the fast fix - wrote this just as a copy of the linked issue on the road.

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

No branches or pull requests

3 participants