title: Laravel 5.2 view composer gets executed multiple times
body: <p>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.</p>
  
  <p>All the queries that get executed multiple times are in my view composer provider.</p>
  
  <p>This is what my view composer looks like:</p>
  
  <pre><code>public function boot()
      {
          view()-&gt;composer('partials.sidebar', function ($view) {
              $currentCategory = $this-&gt;getCurrentCategory();
              $sidebarCategories = SidebarCategory::category($currentCategory)
                  -&gt;order()
                  -&gt;get();
              $view
                  -&gt;with('sidebarCategories', $sidebarCategories);
          });
  
          view()-&gt;composer('partials.footer', function ($view) {
              $footerLinks = FooterCategory::with('links.translations')-&gt;order()-&gt;get();
              $footerColumn1 = $footerLinks-&gt;filter(function ($value, $key) {
                  return $value-&gt;column == 1;
              });
              $footerColumn2 = $footerLinks-&gt;filter(function ($value, $key) {
                  return $value-&gt;column == 2;
              });
              $footerColumn3 = $footerLinks-&gt;filter(function ($value, $key) {
                  return $value-&gt;column == 3;
              });
              $footerColumn4 = $footerLinks-&gt;filter(function ($value, $key) {
                  return $value-&gt;column == 4;
              });
  
              $view
                  -&gt;with(compact('footerColumn1', 'footerColumn2', 'footerColumn3', 'footerColumn4'));
          });
  }
  </code></pre>
  
  <p>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').</p>
  
  <p>I've tried this:</p>
  
  <pre><code>if($view-&gt;offsetExists('sidebarCategory'))
      return;
  </code></pre>
  
  <p>But the offsetExists always returns false (even after its called for 5. time).</p>
  
  <p>Any idea why this is happening and what I'm doing wrong?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>I've realised where the issue is. On the page that I'm visiting (where those multiple queries get executed) there are some 404 elements (images mostly). Each time a file is not found a new 404 exception is thrown. And each time a 404 exception is thrown the 404 view gets executed => meaning footer/sidebar queries get executed as well (as they're a part of 404 view). Example: <a href="https://imgur.com/a/RrmOD" rel="noreferrer">http://imgur.com/a/RrmOD</a></p>
  
  <p>So the follow up question is how to prevent the view from getting rendered when there is no need to (example the 404 is an image that wasnt found).</p>
  
  <p>Here is a piece of code from my routes, that I assume is the reason that this is happening:</p>
  
  <pre><code>Route::get('{slug}', ['as' =&gt; 'findBySlug', 'uses' =&gt; function($slug) {
      if(\App\DynamicCategory::findBySlug($slug)-&gt;count() &gt; 0)
          return App::make('App\Http\Controllers\GeneralController')-&gt;getDynamicCategoryIndex($slug);
      else if(\App\DynamicPage::noCategory()-&gt;findBySlug($slug)-&gt;count() &gt; 0)
          return App::make('App\Http\Controllers\GeneralController')-&gt;getDynamicPage($slug);
      else
          abort(404);
  }]);
  </code></pre>
  
  <p>PS: I know this piece of code is extremely unoptimized (as it basically executes the same query twice, once to see if the item exists, another time actually in the controller). This is work in progress and it's on todo list.</p>
  
  <p><strong>Edit 2:</strong></p>
  
  <p>I've come up with the next solution, I'm open to improvements as this is a somewhat hack-ish way (if I add more folders, I will need to keep in mind to update this as well). I only have 3 direct subfolders in public folder: something, files and resources.</p>
  
  <p>The solution is to check the url's first segment when rendering the exception (in file app/Exceptions/Handler.php) and return 404 response without the view if it matches one of the 3 folders:</p>
  
  <pre><code>public function render($request, Exception $e)
  {
      $firstSegment = $request-&gt;segment(1);
      if(starts_with($firstSegment, 'files') || starts_with($firstSegment, 'something') || starts_with($firstSegment, 'resources')) {
          return response('Stran ne obstaja', 404);
      }
  
      return parent::render($request, $e);
  }
  </code></pre>
  
  <p>Thanks in advance</p>