I'm totally new with Laravel and trying to create simple invoice form (just drafted few fields of it till now). Header of the Invoice is saved in db properly, the first InvoiceItem also. However, if I'm adding more than one row in a table with Invoice items, only the first value is saved in db.
This is InvoiceItem row:
<tr>
<td>
<select class="form-control name" name="productname[]">
<option value="0" selected="true" disabled="true">Select Product</option>
@foreach($product_lists as $key=>$p)
<option value="{!!$key!!}">{!!$p!!}</option>
@endforeach
</td>
<td><input type="text" class="form-control form-control-line qty" name="qty[]"></td>
<td><input type="text" class="form-control form-control-line price" name="price[]"></td>
<td><input type="text" class="form-control form-control-line total" name="total[]"></td>
<td><a href="#" class="remove">-<i class="glyphicon glyphicon-remove"></i></a></td>
</tr>
This is jquery to add another InvoiceItem row:
function addRow(){
var tr='<tr>'+
'<td>'+
'<select class="form-control name" name="productname[]">'+
'<option value="0" selected="true" disabled="true">Select Product</option>'+
'@foreach($product_lists as $key=>$p)'+
'<option value="{!!$key!!}">{!!$p!!}</option>'+
'@endforeach'+
'</td>'+
'<td><input type="text" class="form-control form-control-line qty" name="qty[]"></td>'+
'<td><input type="text" class="form-control form-control-line price" name="price[]"></td>'+
'<td><input type="text" class="form-control form-control-line total" name="total[]"></td>'+
'<td><a href="#" class="remove">-<i class="glyphicon glyphicon-remove"></i></a></td>'+
'</tr>';
$('tbody').append(tr);
};
This is my controller:
public function store(Request $request)
{
$salesinvoice = $this->validate(request(), [
'invoice_no' => 'required'
]);
SalesInvoice::create($salesinvoice);
foreach ($request->productname as $key=>$v) {
$data=array(
'product_id'=>$request->$v,
'qty'=>$request->qty[$key],
'price'=>$request->price[$key],
'total'=>$request->total[$key]
);
SalesInvoiceItem::insert($data);
};
I would appreciate any guidance/hints how to solve it.
from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2rC0mlb
via IFTTT
Aucun commentaire:
Enregistrer un commentaire