Loosing array keys

Dear Experts!
I’m new with Laravel and Livewire and I don’t know, it is working correctly and I’m thinking it wrong or it should be a mistake.

I want to buld a checkbox list based in many-to-many connections of 2 tables.

I’m collecting the data into
$data_paymethodes like this:
$this->data_paymethodes=Paymethode::orderby(‘name’)->pluck(‘name’,‘id’)->toArray();
here i have indexes from starting 1 and last is 7
I keep the data in this variable to list all the selectable options.
It is registered in my controller as public variable.

I have the actual selection of the options in other variable:
$Checkout = Checkout::findOrFail($id);
$this->paymethodes=$Checkout->paymethodes()->pluck(‘name’,‘paymethode_id’)->toArray();
it is working nice within the controller:
$this->paymethodes:
array:2 [▼
3 => “voucher”
5 => “credit card - 2”
]
All options:
$this->data_paymethodes:
array:7 [▼
2 => “transfer”
6 => “credit card - 1”
5 => “credit card - 2”
4 => “others”
1 => “cash”
7 => “cash at courier”
3 => “voucher”
]

In my blade file I’m proessing with froeach the $data_paymethodes variable
BUT it looks to loosing my ID’s (keys in both above arrays)

{{var_export($paymethodes,1)}}
{{var_export($data_paymethodes ,1)}}

In both DUMP the array key starts from 0 and so I lost the array keys what i have stored in the DB.

                        <ul class="list-inside">
                        @foreach($data_paymethodes as $id=>$paymethode)
                                <li>
                                    <input  wire:model="paymethodes"
                                            id="paymethodes.{{ $id }}"
                                            name="paymethodes[]"
                                            type="checkbox"
                                            value="{{$id}}"
                                    />
                                    <label for="paymethodes.{{ $id }}">{{$id}}-{{$paymethode}}</label>
                                </li>
                        @endforeach
                        </ul>

So the checking of the checkboxes are working more than interesting.
Could anyone help me please?
Thank You

br.
Gergely

I’ve experienced this issue and I think it has to do with integer values as keys. If you use a string value, it’ll keep the keys together but for some reason, when using integers, it disregards them as keys. You can work around this for now by using a string identifier like paymethodes.pay{{ $id }}. Not ideal especially if you’re using the array key for some logic in the backend but there are other ways to transfer that data - you’re already using it as your value.

Thank You.
I was worked with this issue 2 days befor I asked for help, so i have had several other trials too.
It is a great idea to test with sting. I try to update with (string) my ID-s
Actual my woriking solution is to use names instead of values and before I’m saving the original array with correct id-name pairs and I search back the correct id for the given name.
In my case the name is also uniques, so it is OK but I think it is not a professional solution :frowning:
Thank You again for your time and affort for the investigation.
Br. Gregely