2016-09-14 32 views
1

我想用選擇選項發送多個數據。示例:剪髮的選項很長或很小。當你是男性時,應該是20分鐘,女性是30分鐘。如何使用laravel中的select選項發送多個數據?

現在我有一個表中有多個數據的值: 第一個值是名稱。第二個是男人和第三個女人的時間。

<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'); 

} 

正如你可以看到我剝|並保存在它自己的變量的一切。然後我檢查它是否設置爲男人或女人。基於此,我爲男人或女人增加時間並更新行。

這個工程,但我認爲這是相當凌亂。這是我應該改變的東西,有人可以鏈接我的例子或解釋我需要改變?

回答

1

首先,您不應該將時間常量從前端發送到後端。你應該更好地將它們硬編碼到後端的某個地方。在我的例子中,我只是用相同的控制器方法將它們硬編碼爲數組。

從前端,你應該只發送選定的髮型/ haircolor類型的名稱。例如kortwit。我還建議你使用小寫字母表示這些值。這就是控制器動作的樣子:

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'); 

} 

這種方式很容易定製它。再也沒有|了。高級用戶也無法將他們的時間值發送到後端。

+0

這使得更多的意義!謝謝,我很高興問。 – twoam

相關問題