title: Creating, Sorting a menu and sub-menu from database in laravel
body: <p>I'm trying to build menu section which is being fetched from the database, I've <code>menus</code> and <code>sub menus</code> too. I've mentioned a <code>column</code> with the name <code>parent_id</code> if <code>NULL</code>  represents it is a <code>parent</code> element if any <code>id</code> is present it is <code>submenu</code> of that particular <code>id of menu element</code>. following is my table</p>
  
  <pre><code>/***************** Menu Elements Table ****************/
  |id |   name     |   link    | parent_id | sort_id |
  ____________________________________________________
  |1  | Home       | home      |    NULL   |   NULL  |
  |2  | About Us   | about-us  |    NULL   |   NULL  |
  |3  | Services   | services  |     2     |     2   |
  |4  | Vision     | vision    |     2     |     1   |
  |5  | Contact us | contact-us|    NULL   |   NULL  |
  </code></pre>
  
  <p>and menu to be in format something like this:</p>
  
  <pre><code>&lt;ul id="main-nav"&gt;
      &lt;li class="menu-item"&gt;&lt;a href="index"&gt;Home&lt;/a&gt;&lt;/li&gt;
      &lt;li class="menu-item"&gt;&lt;a href="about-us"&gt;About Us&lt;/a&gt;
          &lt;ul class="sub-nav"&gt;
              &lt;li&gt;&lt;a href="vision"&gt;Vision&lt;/a&gt;&lt;/li&gt;
              &lt;li&gt;&lt;a href="services"&gt;Services&lt;/a&gt;&lt;/li&gt;
      &lt;/li&gt;
      &lt;li class="menu-item"&gt;&lt;a href="contact-us"&gt;Contact us&lt;/a&gt;&lt;/li&gt;
  &lt;/ul&gt;
  </code></pre>
  
  <p>Menu is also sorted according to the sort_id</p>
  
  <p>Till now I'm able to sort the menu elements but I want the sort to be parent_id specific.</p>
  
  <pre><code>$menu_elements = MenuElements::orderBy('sort_id', 'asc')-&gt;get()-&gt;all();
  </code></pre>
  
  <p>I'm bit confused how to achieve this. Help me out.</p>
  
  <p>Thanks</p>