this error occur because you don't have dynamic column(increasing column ,mean you have to create column every time manually in Db) in you DB so use big data for this kind of thing but still if you need to perform these thing in other data for that you need to store data in row wise rather than column wise hope this help
Jun 27, 2017
31
Level 4
Laravel 5.4 Dynamic form save data to database
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();
}
Level 6
for ($i=0; $i < count($input['quantity']); ++$i)
{
$products= new products;
$products->quantity = $input['quantity'][$i];
$products->description= $input['description'][$i];
$products->selling_price= $input['selling_price'][$i];
$products->save();
}
3 likes
Please or to participate in this conversation.