title: How to get the selected dropdown list in edit modal with Laravel
body: <p>i am building a project with laravel where i am able to fetch the record from database into my modal but the problem is everytime do i update my modal then the options values in both the select also gets incremented with the same fetched result. How to avoid that. Here i am pasting two images before and after of edit and also what i have done till now. Please help me to complete it.</p>
  <p>Before Image</p>
  <p><a href="https://i.stack.imgur.com/wU58D.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wU58D.png" alt="enter image description here" /></a></p>
  <p>After Image</p>
  <p><a href="https://i.stack.imgur.com/fuH5f.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/fuH5f.png" alt="enter image description here" /></a></p>
  <p>My modal part</p>
  <pre><code>&lt;a href=&quot;javascript:void(0);&quot; data-href=&quot;{{ url('admin/product_edit/'.$cat-&gt;id) }}&quot; class=&quot;edit_product btn btn-sm btn-primary&quot; data-toggle=&quot;modal&quot; data-target=&quot;#editModal&quot;&gt; Edit&lt;/a&gt;
  
  &lt;div class=&quot;modal&quot; id=&quot;editModal&quot; tabindex=&quot;-1&quot; role=&quot;dialog&quot; aria-labelledby=&quot;insertModalLabel&quot; aria-hidden=&quot;true&quot;&gt;
                  &lt;div class=&quot;modal-dialog modal-lg&quot; role=&quot;document&quot;&gt;
                      &lt;div class=&quot;modal-content&quot;&gt;
                          &lt;div class=&quot;modal-header&quot;&gt;
                              &lt;h5 class=&quot;modal-title font-weight-bold&quot; id=&quot;insertModalLabel&quot;&gt;EditCustomer&lt;/h5&gt;
                              &lt;button type=&quot;button&quot; class=&quot;close&quot; data-dismiss=&quot;modal&quot; aria-label=&quot;Close&quot;&gt;
                                  &lt;span aria-hidden=&quot;true&quot;&gt;&amp;times;&lt;/span&gt;
                              &lt;/button&gt;
                          &lt;/div&gt;
                          &lt;div class=&quot;modal-body&quot;&gt;
                            &lt;p&gt;&lt;b&gt;Please note:&lt;/b&gt; Fields marked with &lt;span class=&quot;control-label&quot;&gt;&lt;/span&gt; is mandatory&lt;/p&gt;
                            &lt;form action=&quot;{{ url('admin/update_product') }}&quot; method=&quot;post&quot; class=&quot;edit_database_operation&quot;&gt;
                              @csrf
                              &lt;input class=&quot;form-control&quot; type=&quot;hidden&quot; name=&quot;eid&quot; id=&quot;eid&quot;&gt;
                              {{-- &lt;input class=&quot;form-control&quot; type=&quot;hidden&quot; name=&quot;brand_id&quot; id=&quot;brand_id&quot;&gt;
                              &lt;input class=&quot;form-control&quot; type=&quot;hidden&quot; name=&quot;category_id&quot; id=&quot;category_id&quot;&gt; --}}
                              
                              &lt;div class=&quot;form-row&quot;&gt;
                                &lt;div class=&quot;form-group col-md-6&quot;&gt;
                                  &lt;label for=&quot;eproduct_category&quot; class=&quot;control-label&quot;&gt;Product Category&lt;/label&gt;
                                  &lt;select id=&quot;eproduct_category&quot; class=&quot;form-control&quot; name=&quot;eproduct_category&quot;&gt;
                                  &lt;/select&gt;
                                &lt;/div&gt;
                              
                                &lt;div class=&quot;form-group col-md-6&quot;&gt;
                                  &lt;label for=&quot;eproduct_brand&quot; class=&quot;control-label&quot;&gt;Product Brand&lt;/label&gt;
                                  &lt;select id=&quot;eproduct_brand&quot; class=&quot;form-control&quot; name=&quot;eproduct_brand&quot;&gt;
                                  &lt;/select&gt;
                                &lt;/div&gt;
                              &lt;/div&gt;     
                              
                                                
  
                              &lt;button type=&quot;submit&quot; class=&quot;btn btn-primary text-left&quot;&gt;Submit&lt;/button&gt;
                            &lt;/form&gt;
                          &lt;/div&gt;
                          
                      &lt;/div&gt;
                  &lt;/div&gt;
                &lt;/div&gt;
  
  </code></pre>
  <p>The script part</p>
  <pre><code>$(document).on('click','.edit_product',function(e){
      e.preventDefault();
      var url = $(this).attr('data-href');
      console.log(url);
      $.ajax({
          url:url,
          method:&quot;GET&quot;,
          success:function(fb){
              var resp = $.parseJSON(fb);
              console.log(resp);
              $('#eid').val(resp.id);
              var brand_id = resp.brand_id;
              brand_edit(brand_id);
              var category_id = resp.category_id;
              category_edit(category_id);
          }
  
      });
      return false;
  });
  
  function brand_edit(brand_id){
      url = '/admin/edit_product_brand/'+brand_id;
      console.log(url);
      $.ajax({
          url:url,
          method:&quot;GET&quot;,
          success:function(fb){
              console.log(fb);
              $('#eproduct_brand').append(fb);
          }
      });
      return false;
  
  }
  
  function category_edit(category_id){
      url = '/admin/edit_product_category/'+category_id;
      console.log(url);
      $.ajax({
          url:url,
          method:&quot;GET&quot;,
          success:function(fb){
              console.log(fb);
              $('#eproduct_category').append(fb);
          }
      });
      return false;
  
  }
  
  $(document).on('submit','.edit_database_operation',function(e){
      e.preventDefault();
      var url = $(this).attr('action');
      var data = $(this).serialize();
      $.ajax({
          url:url,
          method:&quot;POST&quot;,
          data:data,
          success:function(fb){
              var resp = $.parseJSON(fb);
              // console.log(resp);
              if(resp.status=='true'){
                  toastr.success(resp.message,'Success');
                  $('#editModal').modal('hide');
                  $('.edit_database_operation')[0].reset();
                  $(&quot;#example1&quot;).load(&quot; #example1 &gt; *&quot;);
                  $(&quot;#example2&quot;).load(&quot; #example2 &gt; *&quot;);
                  // $('.edit_database_operation')[0].reset();
                  $(&quot;#eproduct_category&quot;).data(&quot;default-value&quot;,$(&quot;#eproduct_category&quot;).val());
              }else if(resp.status=='false'){
                      
                  $.each( resp.message, function( key , value ) {
                  toastr.error(value + '&lt;br&gt;','Error');
                  });
                  
  
              }else if(resp.status=='false-1'){
                  toastr.error(resp.message,'Error');
              }
  
              
              
              
          }
  
      });
      
      return false;
  });
  
  </code></pre>
  <p>The Controller part</p>
  <pre><code>public function product_edit($id){
          $edit = Product::where('id',$id)-&gt;first();
          $arr = array('id'=&gt;$edit-&gt;id,'category_id'=&gt;$edit-&gt;category_id,'brand_id'=&gt;$edit-&gt;brand_id);
          echo json_encode($arr);
      }
      
      public function edit_product_brand($id){
          $brande = Brand::where('status','active')-&gt;orderBy('id','DESC')-&gt;get();
          $output = '';
          // $output .= '';
          foreach ($brande as $brand){
          $brand_id = $brand-&gt;id;
          $brand_name = $brand-&gt;brand_name;
  
          $output .= '&lt;option value=&quot;'.$brand_id.'&quot; '.(($brand_id == $id) ? 'selected=&quot;selected&quot;':&quot;&quot;).'&gt;'.$brand_name.'&lt;/option&gt;';
  
          }
          // $output .='&lt;/select&gt;';
  
          return $output;
      }
  
      public function edit_product_category($id){
          $category = Category::where('status','active')-&gt;orderBy('id','DESC')-&gt;get();
          $output = '';
          if(!$category-&gt;isEmpty()){
              
              foreach ($category as $brand){
                  $category_id = $brand-&gt;id;
                  $category_name = $brand-&gt;category_name;
          
                  $output .= '&lt;option value=&quot;'.$category_id.'&quot; '.(($category_id == $id) ? 'selected=&quot;selected&quot;':&quot;&quot;).'&gt;'.$category_name.'&lt;/option&gt;';
          
              }
              
          }
  
          return $output;
      }
  
  
      public function update_product(Request $request){
  
          $update = Product::where('id',$request-&gt;eid)-&gt;first();
          $validator = Validator::make($request-&gt;all(),
          [
              
              'eproduct_category'=&gt;'required',
              'eproduct_brand'=&gt;'required',
          ],[
              
              'eproduct_category.required'=&gt;'Please enter product category',
              'eproduct_brand.required'=&gt;'Please enter product brand',
              
          ]);
          if(!$validator-&gt;fails()){
              
                      $update-&gt;name = $request-&gt;eproduct_name;
                      $update-&gt;category_id = $request-&gt;eproduct_category;
                      $update-&gt;brand_id = $request-&gt;eproduct_brand;
                      if($update-&gt;update()){
                          $arr = array('status'=&gt;'true','message'=&gt;'Updated Successfully');
                      }else{
                          $arr = array('status'=&gt;'false-1','message'=&gt;'Not Updated');
                      }
          }else{
              $arr = array('status'=&gt;'false','message'=&gt;$validator-&gt;errors()-&gt;all());
          }
          echo json_encode($arr);
  
      }
  
  </code></pre>