0

I am sending 4 arrays to my controller that they need to combine before I can store them to database, here is how they're look like:

array:4 [▼
  "from" => array:7 [▼
    0 => "8:00"
    1 => null
    2 => null
    3 => null
    4 => null
    5 => null
    6 => null
  ]
  "to" => array:7 [▼
    0 => "21:00"
    1 => null
    2 => null
    3 => null
    4 => null
    5 => null
    6 => null
  ]
  "closed" => array:6 [▼
    0 => "on"
    1 => "on"
    2 => "on"
    3 => "on"
    4 => "on"
    5 => "on"
  ]
  "day_id" => array:7 [▼
    0 => "a0275acb-c6e5-428f-8589-da0644c1f42e"
    1 => "9a8d2dc5-fbe5-4a93-a279-2c6a90781777"
    2 => "84d74f04-3728-4314-a6e7-8710cecaa911"
    3 => "79ef140a-5de5-4b27-a286-9893cbac820e"
    4 => "6d0b2858-ff96-46ce-ae1b-665c3ee353aa"
    5 => "38225fe6-3bce-4ee9-bad8-c7f6cacc4854"
    6 => "28cef2be-1ff6-4e80-b322-a208f4838391"
  ]
]

As you might notice closed array is less by 1 value as the checkbox was unchecked, so this one is vary between no value (if none are check) to 7 values (if all of them are checked)

Anyway, I need to make this arrays to something like this:

array:7 [▼
  "finaldata" => array:4 [▼
    from => "8:00"
    to => "21:00"
    closed => off
    day_id => "a0275acb-c6e5-428f-8589-da0644c1f42e"
  ],
 "finaldata" => array:4 [▼
    from => null
    to => null
    closed => on
    day_id => "9a8d2dc5-fbe5-4a93-a279-2c6a90781777"
  ],
  // and so on...
]

If I can get such result then I'll be able to loop and store this data into my database.

Code

$schedules = [];
$schedules['from'] = $request->input('from');
$schedules['to'] = $request->input('to');
$schedules['closed'] = $request->input('closed');
$schedules['day_id'] = $request->input('day_id');
dd($schedules); // returning my results above (on top)

foreach($schedules as $schedule) {
  $days = new WorkDays;
  //...
  $days->save();
}

Question

How can I get such result as I shared to store my data?

Update

blade

<div class="mt-4">
    <div x-data="handler()">
        <x-jet-label value="Schedule" />
        <table class="itable align-middle w-full">
            <thead class="thead-light">
                <tr>
                    <th>Day</th>
                    <th>From</th>
                    <th>To</th>
                    <th>Closed</th>
                </tr>
            </thead>
            <tbody>
                <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" name="from[]" />
                            <x-jet-input x-model="field.id" type="hidden" value="field" name="day_id[]"/>
                        </td>
                        <td><x-jet-input x-model="field.to" type="text" name="to[]" /></td>
                        <td><input x-model="field.closed" class="form-checkbox" type="checkbox" name="closed[]" /></td>
                    </tr>
                </template>
            </tbody>
        </table>
    </div>
    <script>
        function handler() {
            return {
                fields: []
            }
        }
    </script>
</div>

Update 2

based on iCoders answer this is what i get, which is good but the ones with unchecked checkbox will not have closed variable array 0 and 6.

"data" => array:7 [▼
    0 => array:3 [▼
      "from" => "54"
      "day_id" => "a0275acb-c6e5-428f-8589-da0644c1f42e"
      "to" => "474"
    ]
    1 => array:4 [▼
      "from" => null
      "day_id" => "9a8d2dc5-fbe5-4a93-a279-2c6a90781777"
      "to" => null
      "closed" => "on"
    ]
    2 => array:4 [▼
      "from" => null
      "day_id" => "84d74f04-3728-4314-a6e7-8710cecaa911"
      "to" => null
      "closed" => "on"
    ]
    3 => array:4 [▼
      "from" => null
      "day_id" => "79ef140a-5de5-4b27-a286-9893cbac820e"
      "to" => null
      "closed" => "on"
    ]
    4 => array:4 [▼
      "from" => null
      "day_id" => "6d0b2858-ff96-46ce-ae1b-665c3ee353aa"
      "to" => null
      "closed" => "on"
    ]
    5 => array:4 [▼
      "from" => null
      "day_id" => "38225fe6-3bce-4ee9-bad8-c7f6cacc4854"
      "to" => null
      "closed" => "on"
    ]
    6 => array:3 [▼
      "from" => "67565"
      "day_id" => "28cef2be-1ff6-4e80-b322-a208f4838391"
      "to" => "6567"
    ]
mafortis
  • 6,750
  • 23
  • 130
  • 288
  • You need to tie your form controls together so that you know which checkbox is not checked. You can do this by adding an index to your form control's names. I just answered a question like this. https://stackoverflow.com/questions/64707394/attach-images-to-product-variants-when-creating-a-new-product-laravel/64707895#64707895 – bassxzero Nov 06 '20 at 04:08
  • I think you would have to fix how you send the request data on the frontend. The way you are doing it, you have no way of knowing to which of the registers correspond the elements of the `closed` array . – porloscerros Ψ Nov 06 '20 at 04:10
  • @porloscerrosΨ I've updated my question would you mind point me what should I do? – mafortis Nov 06 '20 at 04:12
  • @bassxzero sorry but I couldn't make relative connection between your answer there and my question here, it didn't help – mafortis Nov 06 '20 at 04:13
  • @mafortis.updated answer – Vision Coderz Nov 06 '20 at 04:39
  • In your php's foreach loop just use `isset($value['closed'])` to see if the value is set. If it's not set just set it to `off`. – bassxzero Nov 06 '20 at 04:45

2 Answers2

1

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'"/>
Vision Coderz
  • 8,257
  • 5
  • 41
  • 57
0

Change your blade to something like this.

<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" name="finaldata[{{$loop->iteration}}]['from']" />
            <x-jet-input x-model="field.id" type="hidden" value="field" name="finaldata[{{$loop->iteration}}]['day_id']"/>
        </td>
        <td><x-jet-input x-model="field.to" type="text" name="finaldata[{{$loop->iteration}}]['to']" /></td>
        <td><input x-model="field.closed" class="form-checkbox" type="checkbox" name="finaldata[{{$loop->iteration}}]['closed']" /></td>
    </tr>
</template>
bassxzero
  • 4,838
  • 22
  • 34