mercredi 26 avril 2017

Eloquent model observer performance

If I have multipile models and I working heavy with events so in my AppServiceProvider I create a observer for my models like:

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        User::observe(UserObserver::class);
        User2::observe(UserObserver2::class);
        User3::observe(UserObserver3::class);
        User4::observe(UserObserver4::class);
        User5::observe(UserObserver5::class);
        User6::observe(UserObserver6::class);
        User7::observe(UserObserver7::class);
        User8::observe(UserObserver8::class);
        User9::observe(UserObserver9::class);
    }

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

does it create some performance penalty? as If I understand the observe method correctly for every app access it will loop through all my models observers method and will match them to eloquent events so if I have 30 models observers it can cause a bad performance?

is there some clever way of declaring the observe class only when the model is in use? so instead of declaring the observers in each app access even when not needed each model will know about its observer only when its in use?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire