I am dynamically adding / removing fields for product properties using jquery. Then the data is converted to json and sent to DB, I get the following json array:
[{"key": "123"},{"value": "21321"}]
But I want to get the next one:
[{"key": "123","value": "21321"}]
<input type="text" name="properties[][key]" class="form-control m-input ml-3" placeholder="Key" autocomplete="off">
<input type="text" name="properties[][value]" class="form-control m-input ml-3" placeholder="Value" autocomplete="off">
This is how the correct array is obtained. But here the number of fields is fixed, while mine is dynamic:
@for ($i=0; $i <= 4; $i++)
<input type="text" name="properties[{{ $i }}][key]" class="form-control" value="{{ old('properties['.$i.'][key]') }}">
<input type="text" name="properties[{{ $i }}][value]" class="form-control" value="{{ old('properties['.$i.'][value]') }}">
@endfor
I also want to display existing values for editing. I do it like this:
@isset($product)
@foreach($product->properties as $prod)
<div class="input-group mb-3">
<input type="text" name="properties[][key]" value="{{ $prod['key'] ?? '' }}" class="form-control m-input editinp-key" placeholder="Key" autocomplete="off">
<input type="text" name="properties[][value]" value="{{ $prod['value'] ?? '' }}" class="form-control m-input ml-3 editinp-value" placeholder="Value" autocomplete="off">
<div class="input-group-append ml-3">
<button id="removeRow" type="button" class="btn btn-danger">Remove</button>
</div>
</div>
@endforeach
@endisset
But for some reason, if, for example, all 2 properties, the output is always 2 times more. I suppose this is due to the incorrect formation of the json array?