title: Laravel - Controller - Dynamic Menu - All Pages
body: <p>I'm making a website where the menu is manageable by the administrator, my question is how to mount that select to display on all pages.</p>
  
  <p>At first I was doing the query of the menus in all actions of the controller, but I would like to optimize this, just do not know how.</p>
  
  <p>My Controller.</p>
  
  <pre><code>&lt;?php
  
  namespace App\Http\Controllers;
  
  use App\Categorias;
  
  use Illuminate\Http\Request;
  
  class FrontendController extends Controller {
  
      public $template = 'default';
  
      // Retorna Navegação
      public function retornaNavegacao(){
  
          $sql = Categorias::where([
                  ['exibir', '=', 1], ['publicado', '=', 1]
              ])
              -&gt;get();
  
          return $sql;
      }
  
      // Página 'Categorias'
      public function categoria(){
  
          return view('frontend.'.$this-&gt;template.'.categorias.index', 
              array(
                  'mainMenu' =&gt; $this-&gt;retornaNavegacao(),
              )
          );
  
      }
  
      // Página 'Produtos'
      public function produto(){
  
          return view('frontend.'.$this-&gt;template.'.produtos.index', 
              array(
                  'mainMenu' =&gt; $this-&gt;retornaNavegacao(),
              )
          );
  
      }
  
      // Página 'Contato'
      public function contato(){
  
          return view('frontend.'.$this-&gt;template.'.contato.index', 
              array(
                  'mainMenu' =&gt; $this-&gt;retornaNavegacao(),
              )
          );
  
      }
  
  }
  </code></pre>
  
  <p>The function retornaNavegacao() only queries the database and returns all registered menus (There is a relationship to search for all subcategories).</p>
  
  <p>However I have to repeat the menu code in all the actions of the controller, I think it might have a smarter way of not having to repeat this code in all actions.</p>