I use add/remove input fields with jquery and have problem when I want to update the values.
In the store() method foreach loop fetches all input fields that I added, while in the update() method foreach loop fetched only first input.
How can I fix this?
I have two tables "attributes(id,name)" and "attribute_option(id,attribute_id,attribute_value").
In edit.balde.php
<script type="text/javascript">
$("#add").click(function(){
addRow();
}}
function addRow(){
$("optionsTable").append('<tr><td><input type="text" name="value[]" class="form-control"</td><td><button type="button" class="remove-tr">Remove</button></td></tr>);
};
$(document).on('click','.remove-tr', function(){
$(this).parents('tr).remove();
});
</script>
<button type="button" id="add">Add row</button>
@foreach($attribute_options as $key=>$option)
<tr>
<td>
<input type="hidden" name="option_id[]" value="{{ $option->id }}">
<input type="text" name="value[]" value="{{ $option->attribute_value }}">
</td>
<td>
<button type="button" class="remove-tr">Remove</button>
</td>
</tr>
@endforeach
in my controller action update()
if($request->has('value)){
$options = $request->value;
$option_id = $request->option_id;
foreach($options as $value){
AttributeOption::where('id','=',$option_id)->update(array(
'attribute_value' => $value
));
}
}