laravel merges arrays into one array

I dont know much usage about new component in laravel 8 but usually common idea is

@foreach($days as $key=>$value)
 <tr>
                        <td x-text="field.name"></td>
                        <td>
                            <x-jet-input x-model="field.from" type="text" name="data[{{$key}}][from]" />
                            <x-jet-input x-model="field.id" type="hidden" value="field" name="data[{{$key}}][day_id]"/>
                        </td>
                        <td><x-jet-input x-model="field.to" type="text" name="data[{{$key}}][to]" /></td>
                        <td><input x-model="field.closed" class="form-checkbox" type="checkbox" name="data[{{$key}}][closed]" /></td>
                    </tr>
                    
                    @endforeach

Then in controller if you dd($request->data) you get merged data

Updated

As per another question answer Dynamically set name attribute of input field in loop with AlpineJS You need to use x-bind:name with a template string:

 <template x-for="(field, index) in {{$days}}" :key="index">
                    <tr>
                        <td x-text="field.name"></td>
                        <td>
                            <x-jet-input x-model="field.from" type="text" x-bind:name="`data[${index}][from]`/>
                            <x-jet-input x-model="field.id" type="hidden" value="field" x-bind:name="`data[${index}][day_id]`"/>
                        </td>
                        <td><x-jet-input x-model="field.to" type="text" x-bind:name="`data[${index}][to]` /></td>
                        <td><input x-model="field.closed" class="form-checkbox" type="checkbox" x-bind:name="`data[${index}][closed]` /></td>
                    </tr>
                </template>

Update2:

<input x-model="field.closed" class="form-checkbox" type="checkbox" name="data[{{$key}}][closed]" @click="$event.target.value = $event.target.checked ? '1' : '2'"/>

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top