選択オプションで複数のデータを送信したい。例:ヘアカットのオプションは長いか小さいです。男性の場合は20分、女性の場合は30分です。
現在、値に複数のデータが含まれるフォームがあります。最初の値は名前です。2番目は男性の時間で、3番目は女性の時間です。
<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' );
}
ご覧のとおり、私はを取り除き、|
すべてを独自の変数に保存します。次に、男性用か女性用かを確認します。これに基づいて、男性または女性の時間を追加し、行を更新します。
これは機能しますが、かなり厄介だと思います。これは私が変更すべきものですか、誰かが私に例をリンクしたり、私が変更する必要があるものを説明したりできますか?
まず第一に、フロントエンドからバックエンドに時定数を送信するべきではありません。バックエンドのどこかにハードコーディングする方がよいでしょう。私の例では、同じコントローラーメソッドで配列としてそれらをハードコーディングしています。
フロントエンドから、選択したヘアスタイル/ヘアカラータイプの名前のみを送信する必要があります。たとえば、kort
またはwit
。また、これらの値には小文字を使用することをお勧めします。コントローラのアクションは次のようになります。
public function store(Request $request)
{
$tasks = Appointment::create($request->all());
/* Get the values */
$gender = $request->input('gender');
$hairstyle = $request->input('hairstyle');
$haircolor = $request->input('haircolor');
// Predefined constants
$hairstyle_times = [
'men' => [
'kort' => 20,
'something_else' => 30,
//...etc
],
'women' => [
'kort' => 30,
'something_else' => 40,
//...etc
],
];
$haircolor_times = [
'men' => [
'wit' => 20,
'something_else' => 30,
//...etc
],
'women' => [
'wit' => 30,
'something_else' => 40,
//...etc
]
];
//Calculate the time and save the appointment
$time = $hairstyle_times[$gender][$hairstyle] + $haircolor_times[$gender][$haircolor];
/* 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' );
}
このように、それをカスタマイズすることは非常に簡単です。|
sにはもう魔法はありません。また、上級ユーザーは時間値を入力してバックエンドに送信することはできません。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加