vendredi 23 décembre 2016

Laravel 5.2 view composer gets executed multiple times

I had some (major) performance issues on a project I'm working on and after logging all the queries that get executed, I realised many of them are executed multiple times and I cannot get to the root of the problem.

All the queries that get executed multiple times are in my view composer provider.

This is what my view composer looks like:

public function boot()
    {
        view()->composer('partials.sidebar', function ($view) {
            $currentCategory = $this->getCurrentCategory();
            $sidebarCategories = SidebarCategory::category($currentCategory)
                ->order()
                ->get();
            $view
                ->with('sidebarCategories', $sidebarCategories);
        });

        view()->composer('partials.footer', function ($view) {
            $footerLinks = FooterCategory::with('links.translations')->order()->get();
            $footerColumn1 = $footerLinks->filter(function ($value, $key) {
                return $value->column == 1;
            });
            $footerColumn2 = $footerLinks->filter(function ($value, $key) {
                return $value->column == 2;
            });
            $footerColumn3 = $footerLinks->filter(function ($value, $key) {
                return $value->column == 3;
            });
            $footerColumn4 = $footerLinks->filter(function ($value, $key) {
                return $value->column == 4;
            });

            $view
                ->with(compact('footerColumn1', 'footerColumn2', 'footerColumn3', 'footerColumn4'));
        });
}

Both of these queries (Sidbar and Footer categories) get executed about 6 times, even though each partials is called exactly once. They are both called in master view with @include('partialname').

I've tried this:

if($view->offsetExists('sidebarCategory'))
    return;

But the offsetExists always returns false (even after its called for 5. time).

Any idea why this is happening and what I'm doing wrong?

Thanks in advance



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire