When you define a name that can be multiple items, you use name="element_name[], I don't think you can even specify an index
Jan 6, 2025
5
Level 63
Write a form to obtain the structure for sync with pivot values
Hello,
I'd like to obtain this structure.
'payments' => [
2 => [
'amount' => 85,
],
5 => [
'amount' => 10,
],
]
I have this code for the form.
<div class="flex flex-col gap-4">
@foreach ($payments as $index => $payment)
<div class="flex items-center">
<input type="hidden" name="payments[{{ $index }}][amount]" value="{{ $payment['amount'] }}">
<x-form.select name="payments[{{ $index }}]" id="{{ 'payment-'.$index }}">
<option :value="null">Choisir un mode de paiement</option>
@foreach ($paymodes as $item)
<option value="{{ $item->id }}" @selected($item->id == old('paymodes.'.$index, $payment['paymode_id']))>{{ $item->name }}</option>
@endforeach
</x-form.select>
<div wire:click="addPayment()">ADD</div>
</div>
@endforeach
</div>
This line is not good.
<x-form.select name="payments[{{ $index }}]" id="{{ 'payment-'.$index }}">
I have tested with <x-form.select name="payments[{{ $index }}][paymode_id]" id="{{ 'payment-'.$index }}">, but it gives me the paymode_id key just after the amount key.
Can you help me correct it ?
Thanks.
V
Level 73
1 like
Please or to participate in this conversation.