title: Get data in select-box dropdown when editing form - Laravel 5.2
body: <p>How would I keep my data in my select box when going back and editing a product?</p>
  
  <p>Here is my form with parent and sub-categories:</p>
  
  <pre class="lang-html prettyprint-override"><code>          &lt;form role="form" method="POST" action="{{ route('admin.product.update', $product-&gt;id) }}"&gt;
                  {{ csrf_field() }}
  
  
                  &lt;div class="col-xs-12 col-sm-6 col-md-6"&gt;
                      &lt;div class="form-group{{ $errors-&gt;has('category') ? ' has-error' : '' }}"&gt;
                          &lt;label&gt;Parent Category&lt;/label&gt;
                          &lt;select class="form-control" name="category" id="category" data-url="{{ url('api/dropdown')}}"&gt;
                              &lt;option value=""&gt;&lt;/option&gt;
                              @foreach($categories as $category)
                                  &lt;option value="{{ $category-&gt;id }}"&gt;{{ $category-&gt;category }}&lt;/option&gt;
                              @endforeach
                          &lt;/select&gt;
                           @if($errors-&gt;has('category'))
                              &lt;span class="help-block"&gt;{{ $errors-&gt;first('category') }}&lt;/span&gt;
                          @endif
                      &lt;/div&gt;
                      &lt;br&gt;
                  &lt;/div&gt;
  
                  &lt;div class="col-xs-12 col-sm-6 col-md-6"&gt;
                      &lt;div class="form-group{{ $errors-&gt;has('cat_id') ? ' has-error' : '' }}"&gt;
                          &lt;label&gt;Sub-Category Category&lt;/label&gt;
                          &lt;select class="form-control" name="cat_id" id="sub_category"&gt;
                              &lt;option value=""&gt;&lt;/option&gt;
                          &lt;/select&gt;
                          @if($errors-&gt;has('cat_id'))
                              &lt;span class="help-block"&gt;{{ $errors-&gt;first('cat_id') }}&lt;/span&gt;
                          @endif
                      &lt;/div&gt;
                      &lt;br&gt;
                  &lt;/div&gt;
  
  
                  &lt;div class="form-group col-md-12"&gt;
                      &lt;button type="submit" class="btn btn-primary waves-effect waves-light"&gt;Edit Product&lt;/button&gt;
                  &lt;/div&gt;
  
              &lt;/form&gt;
  </code></pre>
  
  
  
  <p>Here is my function to get the results to edit form:</p>
  
  <pre class="lang-html prettyprint-override"><code> /**
       * Return the view to edit &amp; Update the Products
       *
       * @param $id
       * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
       */
      public function editProduct($id) {
  
          // Find the product ID
          $product = Product::findOrFail($id);
  
          // Get all Categories where parent_id = NULL
          $categories = Category::whereNull('parent_id')-&gt;get();
  
  
          // Return view with products and categories
          return view('admin.product.edit', compact('product', 'categories'));
  
      }
  </code></pre>
  
  
  
  <p>Here is my Category Model for my categories and sub categories:</p>
  
  <pre class="lang-html prettyprint-override"><code>class Category extends Model {
  
      protected $table = 'categories';
  
      protected $fillable = ['category'];
  
  
      /**
       * One sub category, belongs to a Main Category ( Or Parent Category ).
       *
       * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
       */
      public function parent() {
          return $this-&gt;belongsTo('App\Category', 'parent_id');
      }
  
  
      /**
       * A Parent Category has many sub categories
       *
       * @return \Illuminate\Database\Eloquent\Relations\HasMany
       */
      public function children() {
          return $this-&gt;hasMany('App\Category', 'parent_id');
      }
  
  
      /**
       * One Category can have many Products.
       *
       * @return \Illuminate\Database\Eloquent\Relations\HasMany
       */
      public function product() {
          return $this-&gt;hasMany('App\Product', 'id');
      }
  
  }
  </code></pre>
  
  
  
  <p>Here is my Product Model for my products table:</p>
  
  <pre class="lang-html prettyprint-override"><code>class Product extends Model {
  
      protected $table = 'products';
  
      protected $fillable = [
          'product_name',
          'price',
          'cat_id',
          'featured',
          'brand_id',
      ];
  
  
      /**
       * One Product can have one Category.
       *
       * @return \Illuminate\Database\Eloquent\Relations\HasOne
       */
      public function category() {
          return $this-&gt;hasOne('App\Category', 'id');
      }
  
  
  }
  </code></pre>
  
  
  
  <p>And here is how my categories and products table is set up:
  <a href="https://i.stack.imgur.com/EoxQt.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EoxQt.jpg" alt="My Categories Table"></a></p>
  
  <p><a href="https://i.stack.imgur.com/1uQ11.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/1uQ11.jpg" alt="My Products table"></a></p>
  
  <p>Just to let you know, when I choose a parent category, a ajax call fires off and retrieves all the parent sub-categories. I have not included that in this question. </p>