title: Proper way to make a dynamic navigation in Laravel 5
body: <p>I'm building my first Laravel application using 5.1, it's an ecommerce site.</p>
  
  <p>I've started by creating the "static" pages. I quote because the pages are not dynamically generated from product lists etc, but the html is still retrieved from the database.</p>
  
  <p>I've created a <code>PagesController</code>, a <code>Page</code> model, the <code>pages/index.blade.php</code> and <code>pages/show.blade.php</code> views, as well as a <code>MasterTemplate.blade.php</code> template file.</p>
  
  <p>My <code>routes.php</code> looks like:</p>
  
  <pre><code>$router-&gt;get('/', [
      'uses' =&gt; 'PagesController@index',
      'as' =&gt; 'pages.index'
  ]);
  
  $router-&gt;get('/{page}', [
      'uses' =&gt; 'PagesController@show',
      'as' =&gt; 'pages.show'
  ]);
  </code></pre>
  
  <p>This works fine, I can view my index and individual pages that are in the DB.</p>
  
  <p>My problem occurs when I go to add my navigation. Since I plan on using two different navigation bars (one for user, one for admins), I opted to create a <code>_navDefault.php</code> file to include in the <code>MasterTemplate</code>.</p>
  
  <p>Stripping out the excess html it looks like:</p>
  
  <pre><code>@foreach ($pages as $page)
      &lt;li&gt;{!! link_to_route('pages.show', $page-&gt;title, [$page-&gt;slug]) !!}&lt;/li&gt;
  @endforeach
  </code></pre>
  
  <p>This generates the links just fine and they work. But because my <code>PagesController</code>:</p>
  
  <pre><code>...
  public function index()
  {
      $pages = $this-&gt;page-&gt;get();
      return view('pages.index', compact('pages'));
  }
  ...
  </code></pre>
  
  <p><code>$pages</code> only exists on the <code>index</code> view, and so navigating to a <code>show</code> view gives me an error that <code>$pages</code> is undefined, which makes perfect sense.</p>
  
  <p>I could define <code>$pages</code> in the <code>show</code> method, but I will also have other controllers such as <code>ProductController</code> and <code>CartController</code> that will have their own pages I will need in the navigation bar, and I can't very well include <code>$pages</code>, <code>$products</code>, and <code>$cart</code> in every <code>index</code> and <code>show</code> method for each controller.</p>
  
  <p>I'm still fairly new to MVC so I'm not sure of the best way to handle this, with a nav controller/model or something else.</p>
  
  <p>What is the proper way to achieve dynamic navigation bars using multiple controllers?</p>