title: Laravel - How to specify rows for dynamic input fields based on condition
body: <p>I am using Laravel-5.8 to add dynamic input fields to an invoice. Already, I have written this code:</p>
  
  <p>Controller</p>
  
  <pre><code>public function create()
  {
    $customers = Customer::all();
    $products = Product::all();
    return view('invoice.create', compact('customers','products'));
  }
  
  public function store(Request $request)
  {
  $request-&gt;validate([
      'customer_id' =&gt; 'required',
      'product_id' =&gt; 'required',
      'qty' =&gt; 'required',
      'price' =&gt; 'required',
      'dis' =&gt; 'required',
      'amount' =&gt; 'required',
  ]);
  $invoice = new Invoice();
  $invoice-&gt;customer_id = $request-&gt;customer_id;
  $invoice-&gt;total = 1000;
  $invoice-&gt;save();
  foreach ( $request-&gt;product_id as $key =&gt; $product_id){
      $sale = new Sale();
      $sale-&gt;qty = $request-&gt;qty[$key];
      $sale-&gt;price = $request-&gt;price[$key];
      $sale-&gt;dis = $request-&gt;dis[$key];
      $sale-&gt;amount = $request-&gt;amount[$key];
      $sale-&gt;product_id = $request-&gt;product_id[$key];
      $sale-&gt;invoice_id = $invoice-&gt;id;
      $sale-&gt;save();
   }
   return redirect('invoice/'.$invoice-&gt;id)-&gt;with('message','invoice created Successfully');
  }
  </code></pre>
  
  <p>view</p>
  
  <pre><code>    &lt;form  method="POST" action="{{route('invoice.store')}}"&gt;
  @csrf
     &lt;div class="form-group col-md-3"&gt;
       &lt;label class="control-label"&gt;Customer Name&lt;/label&gt;
       &lt;select name="customer_id" class="form-control"&gt;
       &lt;option&gt;Select Customer&lt;/option&gt;
       @foreach($customers as $customer)
            &lt;option name="customer_id" value="{{$customer-&gt;id}}"&gt;{{$customer-&gt;name}} &lt;/option&gt;
       @endforeach
       &lt;/select&gt;                            
    &lt;/div&gt;
    &lt;div class="form-group col-md-3"&gt;
       &lt;label class="control-label"&gt;Date&lt;/label&gt;
       &lt;input name="date"  class="form-control datepicker"  value="&lt;?php echo date('Y-m-d')?&gt;" type="date" placeholder="Enter your email"&gt;
    &lt;/div&gt;
  
    &lt;table class="table table-bordered"&gt;
      &lt;thead&gt;
         &lt;tr&gt;
             &lt;th scope="col"&gt;Product Name&lt;/th&gt;
             &lt;th scope="col"&gt;Qty&lt;/th&gt;
             &lt;th scope="col"&gt;Price&lt;/th&gt;
             &lt;th scope="col"&gt;Discount&lt;/th&gt;
             &lt;th scope="col"&gt;Amount&lt;/th&gt;
             &lt;th scope="col"&gt;&lt;a class="addRow"&gt;&lt;i class="fa fa-plus"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/th&gt;
           &lt;/tr&gt;
         &lt;/thead&gt;
         &lt;tbody&gt;
             &lt;tr&gt;
               &lt;td&gt;&lt;select name="product_id[]" class="form-control productname" &gt;
                  &lt;option&gt;Select Product&lt;/option&gt;
                @foreach($products as $product)
                     &lt;option name="product_id[]" value="{{$product-&gt;id}}"&gt;{{$product-&gt;name}}&lt;/option&gt;
                @endforeach
                       &lt;/select&gt;&lt;/td&gt;
              &lt;td&gt;&lt;input type="text" name="qty[]" class="form-control qty" &gt;&lt;/td&gt;
              &lt;td&gt;&lt;input type="text" name="price[]" class="form-control price" &gt;&lt;/td&gt;
              &lt;td&gt;&lt;input type="text" name="dis[]" class="form-control dis" &gt;&lt;/td&gt;
             &lt;td&gt;&lt;input type="text" name="amount[]" class="form-control amount" &gt;&lt;/td&gt;
             &lt;td&gt;&lt;a   class="btn btn-danger remove"&gt; &lt;i class="fa fa-remove"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/td&gt;
           &lt;/tr&gt;
         &lt;/tbody&gt;
         &lt;tfoot&gt;
          &lt;tr&gt;
           &lt;td&gt;&lt;/td&gt;
           &lt;td&gt;&lt;/td&gt;
           &lt;td&gt;&lt;/td&gt;
           &lt;td&gt;&lt;b&gt;Total&lt;/b&gt;&lt;/td&gt;
           &lt;td&gt;&lt;b class="total"&gt;&lt;/b&gt;&lt;/td&gt;
           &lt;td&gt;&lt;/td&gt;
          &lt;/tr&gt;
         &lt;/tfoot&gt;
  
      &lt;/table&gt;
      &lt;div &gt;
         &lt;button class="btn btn-primary" type="submit"&gt;Submit&lt;/button&gt;
      &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>javascipt</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;script type="text/javascript"&gt;
      $(document).ready(function(){
          $('tbody').delegate('.productname', 'change', function () {
              var  tr = $(this).parent().parent();
              tr.find('.qty').focus();
          })
          $('tbody').delegate('.productname', 'change', function () {
              var tr =$(this).parent().parent();
              var id = tr.find('.productname').val();
              var dataId = {'id':id};
              $.ajax({
                  type    : 'GET',
                  url     :'{!! URL::route('findPrice') !!}',
                  dataType: 'json',
                  data: {"_token": $('meta[name="csrf-token"]').attr('content'), 'id':id},
                  success:function (data) {
                      tr.find('.price').val(data.sales_price);
                  }
              });
          });
          $('tbody').delegate('.qty,.price,.dis', 'keyup', function () {
              var tr = $(this).parent().parent();
              var qty = tr.find('.qty').val();
              var price = tr.find('.price').val();
              var dis = tr.find('.dis').val();
              var amount = (qty * price)-(qty * price * dis)/100;
              tr.find('.amount').val(amount);
              total();
          });
          function total(){
              var total = 0;
              $('.amount').each(function (i,e) {
                  var amount =$(this).val()-0;
                  total += amount;
              })
              $('.total').html(total);
          }
          $('.addRow').on('click', function () {
              addRow();
          });
          function addRow() {
              var addRow = '&lt;tr&gt;\n' +
                  '         &lt;td&gt;&lt;select name="product_id[]" class="form-control productname " &gt;\n' +
                  '         &lt;option value="0" selected="true" disabled="true"&gt;Select Product&lt;/option&gt;\n' +
  '                                        @foreach($products as $product)\n' +
  '                                            &lt;option value="{{$product-&gt;id}}"&gt;{{$product-&gt;name}}&lt;/option&gt;\n' +
  '                                        @endforeach\n' +
                  '               &lt;/select&gt;&lt;/td&gt;\n' +
  '                                &lt;td&gt;&lt;input type="text" name="qty[]" class="form-control qty" &gt;&lt;/td&gt;\n' +
  '                                &lt;td&gt;&lt;input type="text" name="price[]" class="form-control price" &gt;&lt;/td&gt;\n' +
  '                                &lt;td&gt;&lt;input type="text" name="dis[]" class="form-control dis" &gt;&lt;/td&gt;\n' +
  '                                &lt;td&gt;&lt;input type="text" name="amount[]" class="form-control amount" &gt;&lt;/td&gt;\n' +
  '                                &lt;td&gt;&lt;a   class="btn btn-danger remove"&gt; &lt;i class="fa fa-remove"&gt;&lt;/i&gt;&lt;/a&gt;&lt;/td&gt;\n' +
  '                             &lt;/tr&gt;';
              $('tbody').append(addRow);
          };
          $('.remove').live('click', function () {
              var l =$('tbody tr').length;
              if(l==1){
                  alert('you cant delete last one')
              }else{
                  $(this).parent().parent().remove();
              }
          });
      });
  &lt;/script&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>At the moment, when I click the '+' button three times I should have something link this in the diagram:</p>
  
  <p><a href="https://i.stack.imgur.com/uGrcP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/uGrcP.png" alt="dynamic input fields"></a></p>
  
  <p>Now, what I want to achieve is this></p>
  
  <ol>
  <li><p>if(Auth::user()->is_hod == '0'), the number of rows for the dynamic input fields should not be more than three (3) as shown in the diagram.</p></li>
  <li><p>if(Auth::user()->is_hod == '1'), The number of rows for or the dynamic input fields can be more than three (3)</p></li>
  <li><p>If customer name from the the dropdownlist is David, the total quantity from Qty should not be more than 10</p></li>
  </ol>
  
  <p>How do I adjust my code to achieve this.</p>
  
  <p>Thank you.</p>