title: Laravel 5.6 dynamic pages from a database best practice
body: <p>I am using/learning Laravel 5.6 and wondered if this is the best approach in trying to accomplish dynamic pages from a database.</p>
  
  <p>The approach I have taken works but I feel it could be improved especially with having to retrieve the pages for the navigation bar with every request.</p>
  
  <p>I have a route in web.php</p>
  
  <pre><code>Route::get('/', 'PageController@index')-&gt;name('index');
  Route::get('/{page}', 'PageController@show');
  </code></pre>
  
  <p>I then have my page controller with index and show functions. </p>
  
  <pre><code> public function index()
  {
      $pages = Page::all();
      $posts = Post::latest('created_at')-&gt;paginate(2);
      return view('index', compact('posts','pages'));
  }
  
  public function show($uri)
  {
      $pages = Page::all();
      $page = Page::where('uri', $uri)-&gt;first();
      return view('templates.page', compact('page','pages'));
  }
  </code></pre>
  
  <p>Now in my header.blade.php I display the list of pages like this: </p>
  
  <pre><code>@foreach($pages as $page)
       &lt;li class="nav-item"&gt;
            &lt;a class="nav-link" href="/{{ $page-&gt;uri }}"&gt;{{ $page-&gt;title }}&lt;/a&gt;
       &lt;/li&gt;
  @endforeach
  </code></pre>
  
  <p>Now my problem is with all the other controllers I have to get the page's information from the database everytime which seems inefficient.</p>
  
  <p>Any advice would be appreciated. Thank you in advance.</p>