【发布时间】:2020-11-06 04:00:53
【问题描述】:
我正在向我的控制器发送 4 个数组,它们需要在我将它们存储到数据库之前组合起来,它们的外观如下:
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"
]
]
您可能会注意到
closed数组由于未选中复选框而减少了 1 个值,所以这个值在无值(如果没有被选中)到 7 个值(如果所有值都被选中)之间变化
无论如何,我需要把这个数组变成这样:
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...
]
如果我能得到这样的结果,那么我将能够循环并将这些数据存储到我的数据库中。
代码
$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();
}
问题
我怎样才能得到我分享的结果来存储我的数据?
更新
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>
更新 2
基于iCoders 回答这是我得到的,这很好,但未选中复选框的将没有closed 变量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"
]
【问题讨论】:
-
您需要将表单控件绑定在一起,以便知道哪个复选框未被选中。您可以通过将索引添加到表单控件的名称来做到这一点。我刚刚回答了这样一个问题。 stackoverflow.com/questions/64707394/…
-
我认为您必须修复在前端发送请求数据的方式。你这样做的方式,你无法知道哪些寄存器对应于
closed数组的元素。 -
@porloscerrosΨ 我已经更新了我的问题,你介意指出我该怎么做吗?
-
@bassxzero 抱歉,但我无法在您的答案和我的问题之间建立相对联系,这没有帮助
-
@mafortis.updated 答案