I am having an issue with retrieving and saving data from my JS view to my controller and database. I can successfully retrieve the selected items' IDs using item_id
.
However, when I enter quantities for the selected items, the quantity saved in the database is always the first quantity entered. For example:
Item: HpBook
Qty : 5
Item: Mac
Qty: 10
In the database, both items have a quantity of 5 instead of their respective quantities. What could be causing this issue?
JS
$('.form').append(
'<div class="container"> '+
'<input type="hidden" value='+item.id+' data-id="'+item.id+'" name="item_id[]" />'+
'<input type="text" class="quantity" placeholder=" Enter Value " name="quantity" />'+
'<p class="total">Figures $:<span name="figures" id="figures"></span></p>'+
'</div>'
);
Controller
$item = new Item(array(
'total' => $request->get('figures'),
));
$item->save();
$item->products()->attach($request->get('item_id'),['quantity' => $request->get('quantity')]);