【发布时间】:2017-01-22 10:27:50
【问题描述】:
我想通过选择选项发送多个数据。示例:理发的选项是长的还是小的。如果你是男人,应该是 20 分钟,女人应该是 30 分钟。
现在我有一个表单,其值中有多个数据: 第一个值是名称。第二个是男人的时间,第三个是女人的时间。
<div class="radio">
<label>
<input type="radio" name="hairstyle" id="optionsRadios2" value="Kort|20|30">Kort
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="haircolor" value="Wit|20|30">Wit
</label>
</div>
在我的store function 中,我剥离数据并根据性别进行更新
public function store(Request $request)
{
$tasks = Appointment::create($request->all());
/* Get gender value */
$gender = $request->input('gender');
/* Get hair options values */
$hairstyle = $request->input('hairstyle');
$haircolor = $request->input('haircolor');
/* Strip hair options values */
$hs_values = explode('|', $hairstyle);
$hairstyle_choice = $hs_values[0];
$hairstyle_time_men = $hs_values[1];
$hairstyle_time_woman = $hs_values[2];
$hc_values = explode('|', $haircolor);
$haircolor_choice = $hc_values[0];
$haircolor_time_men = $hc_values[1];
$haircolor_time_woman = $hc_values[2];
/* Check if gender = men */
if ($gender == 'men'){
$time = $hairstyle_time_men + $haircolor_time_men;
}
/* Woman */
else{
$time = $hairstyle_time_woman + $haircolor_time_woman;
}
/* Update the new values in the row with id .. */
$titles = DB::table('appointments')
->where('id', $tasks->id)
->update([
'hairstyle' => $hairstyle_choice,
'haircolor' => $haircolor_choice,
'time_costs' => $time,
]);
return redirect( '/appointments' );
}
如您所见,我剥离了 | 并将所有内容保存在它自己的变量中。然后我检查它是否设置为男性或女性。基于此,我为男人或女人添加时间并更新行。
这可行,但我认为它很混乱。这是我应该改变的东西吗,有人可以链接我一个例子或解释我需要改变什么吗?
【问题讨论】:
标签: php database forms laravel