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"
]