title: Generate drop-down list input with values from DB table
body: <p>I am trying to generate a drop-down list with values from a MySQL table using Laravel. The table is simple, two columns - <code>id</code> and <code>category</code>.</p>
  
  <p>The following will retrieve all of the records (categories) but returns an object rather than an array, which is what I need for the drop-down code - </p>
  
  <pre><code>$categories = Category::all();
  </code></pre>
  
  <p>The code for the drop-down is:</p>
  
  <pre><code>{{ Form::select('category', $categories, $post-&gt;category_id) }}
  </code></pre>
  
  <p>Ideas?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>bgallagh3r suggested using a foreach loop to convert each category to an array. Their code got me close but generated a bunch of funky nested <code>optgroup</code>tags. I was able to get it down to just one <code>optgroup</code> but that is one too many..</p>
  
  <pre><code>$categories = Category::all();
  foreach ($categories as $cat)
  {
      $category = $cat-&gt;to_array();
      $id = $category['id'];
      $value = $category['category'];
      $cats[] = array($id =&gt; $value);
  }
  </code></pre>
  
  <p>And then, in the form:</p>
  
  <pre><code>{{ Form::select('categories', $categories)}}
  </code></pre>
  
  <p>I end up with this HTML:</p>
  
  <pre><code>    &lt;select name="categories"&gt;
      &lt;optgroup label="0"&gt;
      &lt;option value="1"&gt;Department News&lt;/option&gt;
      &lt;/optgroup&gt;
      &lt;optgroup label="1"&gt;
      &lt;option value="2"&gt;General&lt;/option&gt;
      &lt;/optgroup&gt;
     ...
      &lt;/select&gt;
  </code></pre>