user15250555
user15250555

Reputation:

The json array is not formed correctly. Laravel

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?

Upvotes: 0

Views: 207

Answers (1)

miken32
miken32

Reputation: 42713

When you give an HTML element name="properties[][key]" you are creating a new element on the properties array, which is an array with key key. So when the next element is named name="properties[][value]" then you add another new element onto the properties array. It's the same in PHP as this code, where the [] creates a new array element:

$properties[]["key"] = 123;
$properties[]["value"] = 21321;

To keep these values together, you can use a numbered array entry instead of creating a new one. Try this instead:

@foreach($product->properties as $i=>$prod)
    <input type="text" name="properties[{{ $i }}][key]" class="form-control m-input ml-3" placeholder="Свойство" autocomplete="off">
    <input type="text" name="properties[{{ $i }}][value]" class="form-control m-input ml-3" placeholder="Значение" autocomplete="off">
@endforeach

Assuming $product->properties is numerically indexed, you'll use $i as a counter variable and should end up with the format you're looking for. This is like this PHP code, where the same array element gets the keys added to it.

$i = 1;
$properties[$i]["key"] = 123;
$properties[$i]["value"] = 21321;

Upvotes: 1

Related Questions