Set array of values to a component from array of inputs usingsame wire:model

I have a wire:model called public $reasons = array();

And I want to fill that wire:model with data from array of inputs.

Let’s say I want to loop over from array of $reasons_arr to generate array of inputs(holding each value from loop) and put it in $reasons. I tried with wire:model=“reasons” but field gets empty, and also tried wire:model=“reasons[]” input fields displays the values but if I diedump over $reasons, I get an empty array.
Hope you could help me with this.

1 Like

Use dot notation for arrays. https://laravel-livewire.com/docs/properties#nested-binding

1 Like

Array was not a collection it’s just a simple array. Something like this array(0=>‘The value’, 1=> ‘Another value’);

You use dot notation on array.key. So wire:model="reasons.0", wire:model="reasons.1" and so on.

To loop:

@foreach ($this->reasons as $key => $item)
    <input wire:model='reasons.{{ $key }}' type="text" />
@endforeach
4 Likes

but when adding same content without any change, new item not added to array?

thanks,solution my proplem!

But Array validation not working </3