nanadjei2
2 years ago
1750
31
Laravel

Laravel 5.4 Dynamic form save data to database

Posted 2 years ago by nanadjei2

I am building a Point of Sale system for a pharmaceutical company. I am allowing the users to add more fields when a new stock is purchased and they want to add them to the existing ones. But I want to submit all of them at once no matter the number of fields they add. I am finding it difficult to do that in the controller. I get this error anytime I try submitting the form Type error: Argument 1 passed to Illuminate\Database\Grammar::parameterize() must be of the type array, string given, called in

This is my HTML

 <div class="container">
     <div class="">
     <div class="row">
       <div class="col-md-8 col-md-offset-2 shad-content">
         <div class="panel-heading "><h3>Please add new drugs</h3></div>
             <div class="panel-body">
              <form action="{{ route('post-new-products') }}" method="POST">
                 {{ csrf_field() }}
                 <table id="add-me" class="table table-bordered">
                <thead>
                  <tr>
                   <th>Quantity</th>
                   <th>Description</th>
                   <th>Selling Price</th>
                   <th>Actions</th>
                 </tr>
              </thead>
              <tbody >
                <tr>
                <td id="quantity" class="col-md-2"><input onkeypress='return event.charCode >= 48 && event.charCode <=57' type="text" name="quantity[]" class="form-control" autofocus="" /></td>
                <td class="col-md-7"><input type="text" name="description[]" class="form-control" /></td>
                <td class="col-md-3"><input type="text" name="selling_price[]" class="form-control" /></td>
                <td class="col-md-2">
                    <button type="button" class="btn btn-danger">
                    Delete</button>
                </td>
              </tr>
            </tbody>
        </table>
        <div class="action-buttons">
            <button id="add-form" type="button" class="btn btn-default">Add New Form</button>
            <button type="submit" class="btn btn-success">Save All Drugs</button>
        </div>
        </form>
      </div>
    </div>
  </div>    
</div>

This is jQuery to add form dynamically

  $('#add-form').click(function() {
       i++;
        $('#add-me').append(
           '<tbody id="row'+i+'"><tr>'+
             '<td class="col-md-2">'+
                '<input id="quantity" onkeypress="return event.charCode >= 48 && event.charCode <=57" type="text" name="quantity[]" class="form-control"/>'
            +'</td>'
            +'<td class="col-md-7">'
                +'<input type="text" name="description[]" class="form-control"/>'
            +'</td>'
            +'<td class="col-md-3">'
                +'<input type="text" name="selling_price[]" class="form-control" />'
            +'</td>'
            +'<td class="col-md-2">'
                +'<button id="'+i+'" type="button" class="btn btn-danger delegated-btn">Delete</button>'
            +'</td>'
        +'</tr></tbody>'
        );
  });

This is my CONTROLLER

  public function createInventory(Request $request) {
      $product = new Product;
      $product->quantities = $request->get('quantity');
      $product->description = $request->get('description');
      $product->selling_price = $request->get('selling_price');
  $product->save();
} 

Please sign in or create an account to participate in this conversation.